Passed
Push — develop ( a27dd2...8f2abb )
by Edwin
03:09
created

Client::getBaseUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
crap 2
1
<?php
2
3
namespace ShopifyClient;
4
5
use ShopifyClient\Auth\OAuth;
6
use ShopifyClient\Resource\AbandonedCheckout;
7
use ShopifyClient\Resource\Blog;
8
use ShopifyClient\Resource\CarrierService;
9
use ShopifyClient\Resource\Country;
10
use ShopifyClient\Resource\Customer;
11
use ShopifyClient\Resource\FulfillmentService;
12
use ShopifyClient\Resource\Order;
13
use ShopifyClient\Resource\Page;
14
use ShopifyClient\Resource\PriceRule;
15
use ShopifyClient\Resource\Product;
16
use ShopifyClient\Resource\Resource;
17
use ShopifyClient\Resource\Shop;
18
use ShopifyClient\Resource\Webhook;
19
20
/**
21
 * @property AbandonedCheckout $abandonedCheckouts
22
 * @property Blog $blog
23
 * @property CarrierService $carrierServices
24
 * @property Country $countries
25
 * @property Order $orders
26
 * @property FulfillmentService $fulfillmentServices
27
 * @property Page $pages
28
 * @property PriceRule $priceRules
29
 * @property Product $products
30
 * @property Customer $customers
31
 * @property Shop $shop
32
 * @property Webhook $webhooks
33
 */
34
class Client
35
{
36
    const API_URL = 'https://%s:%s@%s';
37
38
    /**
39
     * @var Config
40
     */
41
    private $config;
42
43
    /**
44
     * @var ResourceCollection
45
     */
46
    private $resources;
47
48
    /**
49
     * Client constructor.
50
     * @param Config $config
51
     */
52 4
    public function __construct(Config $config)
53
    {
54 4
        $this->config = $config;
55
56 4
        $this->initializeHttpClient();
57 2
    }
58
59 4
    private function initializeHttpClient()
60
    {
61
        $config = [
62 4
            'base_uri' => $this->getBaseUrl()
63
        ];
64
65 4
        if (!empty($this->config->getAccessToken())) {
66 1
            $config['headers'] = [
67 1
                'X-Shopify-Access-Token' => $this->config->getAccessToken(),
68
            ];
69
        }
70
71 4
        $this->resources = new ResourceCollection(new \GuzzleHttp\Client($config), $this->config->getResources());
72 2
    }
73
74
    /**
75
     * @return string
76
     */
77 4
    private function getBaseUrl(): string
78
    {
79 4
        if ($this->config->getAccessToken()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->config->getAccessToken() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
80 1
            return $this->config->getDomain();
81
        }
82
83 3
        return sprintf(
84 3
            self::API_URL,
85 3
            $this->config->getKey(),
86 3
            $this->config->getSecret(),
87 3
            $this->config->getDomain()
88
        );
89
    }
90
91
    /**
92
     * @param $name
93
     * @return Resource
94
     */
95 2
    public function getResource(string $name): Resource
96
    {
97 2
        return $this->resources->getResource($name);
98
    }
99
100
    /**
101
     * @param $name
102
     * @return Resource
103
     */
104 97
    public function __get(string $name): Resource
105
    {
106 97
        return $this->resources->getResource($name);
107
    }
108
}
109