Test Failed
Push — develop ( 9e3f9b...05a047 )
by Edwin
02:28
created

ResourceCollection::getResource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace ShopifyClient;
4
5
use GuzzleHttp\ClientInterface;
6
use ShopifyClient\Exception\ResourceException;
7
use ShopifyClient\Resource\Customer;
8
use ShopifyClient\Resource\CustomerAddress;
9
use ShopifyClient\Resource\Order;
10
use ShopifyClient\Resource\OrderRisk;
11
use ShopifyClient\Resource\Product;
12
use ShopifyClient\Resource\ProductImage;
13
use ShopifyClient\Resource\ProductVariant;
14
use ShopifyClient\Resource\Resource;
15
16
class ResourceCollection
17
{
18
    private $defaultResources = [
19
        'customers' => [
20
            'class'     => Customer::class,
21
            'resources' => [
22
                'addresses' => [
23
                    'class' => CustomerAddress::class,
24
                ],
25
            ],
26
        ],
27
        'orders'    => [
28
            'class'     => Order::class,
29
            'resources' => [
30
                'risks' => [
31
                    'class' => OrderRisk::class,
32
                ],
33
            ],
34
        ],
35
        'products'  => [
36
            'class'     => Product::class,
37
            'resources' => [
38
                'variants' => [
39
                    'class' => ProductVariant::class,
40
                ],
41
                'images'   => [
42
                    'class' => ProductImage::class,
43
                ],
44
            ],
45
        ],
46
    ];
47
48
    /**
49
     * @var array
50
     */
51
    private $resources = [];
52
53
    /**
54
     * ResourceCollection constructor.
55
     * @param ClientInterface $httpClient
56
     * @param array $resources
57
     */
58 3
    public function __construct(ClientInterface $httpClient, array $resources = [])
59
    {
60 3
        $resources = array_merge($this->defaultResources, $resources);
61
62 3
        $this->validateResources($resources);
63 1
        $this->loadResources($httpClient, $resources);
64 1
    }
65
66
    /**
67
     * @param string $key
68
     * @return Resource
69
     * @throws ResourceException
70
     */
71 44
    public function getResource(string $key): Resource
72
    {
73 44
        if (!isset($this->resources[$key])) {
74 1
            throw new ResourceException(sprintf('Resources %s does not exist', $key));
75
        }
76
77 43
        return $this->resources[$key];
78
    }
79
80
    /**
81
     * @param array $resources
82
     * @throws ResourceException
83
     */
84 3
    private function validateResources(array $resources)
85
    {
86 3
        foreach ($resources as $key => $resource) {
87 3
            if(!is_array($resource)) {
88 1
                throw new ResourceException(sprintf('Resource should be an array for resource %s', $key));
89
            }
90
91 3
            $this->validateResource($key, $resource);
92
        }
93 3
    }
94
95
    /**
96
     * @param string $key
97
     * @param array $resource
98
     * @throws ResourceException
99
     */
100 3
    private function validateResource(string $key, array $resource)
101
    {
102 3
        if (!array_key_exists('class', $resource)) {
103 1
            throw new ResourceException(sprintf('Required key "class" is missing for resource %s.', $key));
104
        }
105
106 3
        if (isset($resource['resources'])) {
107 3
            $this->validateResources($resource['resources']);
108
        }
109 3
    }
110
111
    /**
112
     * @param ClientInterface $httpClient
113
     * @param array $resources
114
     */
115 1
    private function loadResources(ClientInterface $httpClient, array $resources)
116
    {
117 1
        foreach ($resources as $key => $resource) {
118 1
            if (array_key_exists($key, $resources)) {
119 1
                $this->loadResource($key, $resource, $httpClient);
120
            }
121
        }
122 1
    }
123
124
    /**
125
     * @param $key
126
     * @param $resource
127
     * @param $httpClient
128
     */
129 1
    private function loadResource($key, $resource, $httpClient)
130
    {
131 1
        $this->resources[$key] = new $resource['class']($httpClient);
132
133 1
        if (isset($resource['resources'])) {
134 1
            $this->loadSubResources($key, $resource, $httpClient);
135
        }
136 1
    }
137
138
    /**
139
     * @param $key
140
     * @param $resource
141
     * @param $httpClient
142
     */
143 1
    private function loadSubResources($key, $resource, $httpClient)
144
    {
145 1
        foreach ($resource['resources'] as $subKey => $subResource) {
146 1
            $this->loadSubResource($key, $subKey, $subResource, $httpClient);
147
        }
148 1
    }
149
150
    /**
151
     * @param $parent
152
     * @param $key
153
     * @param $resource
154
     * @param $httpClient
155
     */
156 1
    private function loadSubResource($parent, $key, $resource, $httpClient)
157
    {
158 1
        if (property_exists($this->resources[$parent], $key)) {
159 1
            $this->resources[$parent]->{$key} = new $resource['class']($httpClient);
160
        }
161 1
    }
162
}
163