Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
13 / 13
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractShippingClient
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
13 / 13
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 setClient
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getClient
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasClient
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getApiUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 setProduction
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isProduction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setProdApiUrl
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setTestApiUrl
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getProdApiUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTestApiUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasProdApiUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasTestApiUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Shipping\Client;
16
17use Pop\Http\Client;
18
19/**
20 * Pop shipping client abstract class
21 *
22 * @category   Pop
23 * @package    Pop\Shipping
24 * @author     Nick Sagona, III <dev@noladev.com>
25 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    4.0.0
28 */
29abstract class AbstractShippingClient implements ShippingClientInterface
30{
31
32    /**
33     * API URL Constants
34     */
35    const FEDEX_PROD_API_URL = 'https://apis.fedex.com';
36    const FEDEX_TEST_API_URL = 'https://apis-sandbox.fedex.com';
37    const UPS_PROD_API_URL   = 'https://onlinetools.ups.com';
38    const UPS_TEST_API_URL   = 'https://wwwcie.ups.com';
39    const USPS_PROD_API_URL  = 'https://api.usps.com';
40    const USPS_TEST_API_URL  = 'https://api-cat.usps.com';
41
42    /**
43     * HTTP client
44     * @var ?Client
45     */
46    protected ?Client $client = null;
47
48    /**
49     * Is production flag
50     * @var bool
51     */
52    protected bool $isProd = false;
53
54    /**
55     * Production API URL
56     * @var ?string
57     */
58    protected ?string $prodApiUrl = null;
59
60    /**
61     * Test API URL
62     * @var ?string
63     */
64    protected ?string $testApiUrl = null;
65
66    /**
67     * Constructor
68     *
69     * Instantiate the shipping auth object
70     */
71    public function __construct(?Client $client = null)
72    {
73        if ($client !== null) {
74            $this->setClient($client);
75        }
76    }
77
78    /**
79     * Set the HTTP client
80     *
81     * @param  Client $client
82     * @return AbstractShippingClient
83     */
84    public function setClient(Client $client): AbstractShippingClient
85    {
86        $this->client = $client;
87        return $this;
88    }
89
90    /**
91     * Get the HTTP client
92     *
93     * @return ?Client
94     */
95    public function getClient(): ?Client
96    {
97        return $this->client;
98    }
99
100    /**
101     * Has HTTP client
102     *
103     * @return bool
104     */
105    public function hasClient(): bool
106    {
107        return ($this->client !== null);
108    }
109
110    /**
111     * Get API URL
112     *
113     * @return ?string
114     */
115    public function getApiUrl(): ?string
116    {
117        return ($this->isProduction()) ? $this->prodApiUrl : $this->testApiUrl;
118    }
119
120    /**
121     * Set as production
122     *
123     * @param  bool $prod
124     * @return AbstractShippingClient
125     */
126    public function setProduction(bool $prod): AbstractShippingClient
127    {
128        $this->isProd = $prod;
129        return $this;
130    }
131
132    /**
133     * Is production
134     *
135     * @return bool
136     */
137    public function isProduction(): bool
138    {
139        return $this->isProd;
140    }
141
142    /**
143     * Set production API URL
144     *
145     * @param  string $prodApiUrl
146     * @return AbstractShippingClient
147     */
148    public function setProdApiUrl(string $prodApiUrl): AbstractShippingClient
149    {
150        $this->prodApiUrl = $prodApiUrl;
151        return $this;
152    }
153
154    /**
155     * Set test API URL
156     *
157     * @param  string $testApiUrl
158     * @return AbstractShippingClient
159     */
160    public function setTestApiUrl(string $testApiUrl): AbstractShippingClient
161    {
162        $this->testApiUrl = $testApiUrl;
163        return $this;
164    }
165
166    /**
167     * Get production API URL
168     *
169     * @return ?string
170     */
171    public function getProdApiUrl(): ?string
172    {
173        return $this->prodApiUrl;
174    }
175
176    /**
177     * Get test API URL
178     *
179     * @return ?string
180     */
181    public function getTestApiUrl(): ?string
182    {
183        return $this->testApiUrl;
184    }
185
186    /**
187     * Has production API URL
188     *
189     * @return bool
190     */
191    public function hasProdApiUrl(): bool
192    {
193        return !empty($this->prodApiUrl);
194    }
195
196    /**
197     * Has test API URL
198     *
199     * @return bool
200     */
201    public function hasTestApiUrl(): bool
202    {
203        return !empty($this->testApiUrl);
204    }
205
206}