Passed
Push — develop ( 7cf2f7...8877c5 )
by Edwin
03:52
created

Client::initializeHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ShopifyClient;
4
5
use ShopifyClient\Resource\AbandonedCheckouts;
6
use ShopifyClient\Resource\Blog;
7
use ShopifyClient\Resource\Country;
8
use ShopifyClient\Resource\Customer;
9
use ShopifyClient\Resource\FulfillmentService;
10
use ShopifyClient\Resource\Order;
11
use ShopifyClient\Resource\PriceRule;
12
use ShopifyClient\Resource\Product;
13
use ShopifyClient\Resource\Resource;
14
use ShopifyClient\Resource\ResourceCollection;
15
use ShopifyClient\Resource\Shop;
16
use ShopifyClient\Resource\Webhook;
17
18
/**
19
 * @property AbandonedCheckouts $abandonedCheckouts
20
 * @property Blog $blogs
21
 * @property Country $countries
22
 * @property Customer $customers
23
 * @property FulfillmentService $fulfillmentServices
24
 * @property PriceRule $priceRules
25
 * @property Product $products
26
 * @property Order $orders
27
 * @property Shop $shop
28
 * @property Webhook $webhooks
29
 */
30
class Client
31
{
32
    const API_URL = 'https://%s/admin/';
33
34
    /**
35
     * @var Request
36
     */
37
    protected $request;
38
39
    /**
40
     * @var Config
41
     */
42
    private $config;
43
44
    /**
45
     * @var ResourceCollection
46
     */
47
    private $resources;
48
49
    /**
50
     * Client constructor.
51
     * @param Config $config
52
     */
53 5
    public function __construct(Config $config)
54
    {
55 5
        $this->config = $config;
56
57 5
        $this->initializeHttpClient();
58 4
    }
59
60 5
    private function initializeHttpClient()
61
    {
62 5
        $this->resources = new ResourceCollection(
63 5
            new Request(new \GuzzleHttp\Client([
64 5
                'base_uri' => $this->getBaseUrl(),
65
                'headers'  => [
66 5
                    'Content-Type'    => 'application/json',
67 5
                    'Accept-Encoding' => 'application/json',
68 5
                    'User-Agent'      => $this->getBaseUrl(),
69 5
                    'Authorization'   => 'Basic ' . $this->getCredentials(),
70
                ]
71
            ])),
72 5
            $this->config->getResources()
73
        );
74 4
    }
75
76
    /**
77
     * @return string
78
     */
79 5
    private function getBaseUrl(): string
80
    {
81 5
        if (!empty($this->config->getAccessToken())) {
82 1
            return $this->config->getDomain();
83
        }
84
85 4
        return sprintf(self::API_URL, $this->config->getDomain());
86
    }
87
88
    /**
89
     * @return string
90
     */
91 5
    private function getCredentials(): string
92
    {
93 5
        return base64_encode(sprintf(
94 5
            '%s:%s',
95 5
            $this->config->getKey(),
96 5
            $this->config->getSecret()
97
        ));
98
    }
99
100
    /**
101
     * @param $name
102
     * @return Resource
103
     */
104 2
    public function getResource(string $name): Resource
105
    {
106 2
        return $this->resources->getResource($name);
107
    }
108
109
    /**
110
     * @param $name
111
     * @return Resource
112
     */
113 133
    public function __get(string $name): Resource
114
    {
115 133
        return $this->resources->getResource($name);
116
    }
117
}
118