Passed
Pull Request — master (#9)
by Mariusz
02:39
created

DataProvider::__construct()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 44
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 35
nc 10
nop 0
dl 0
loc 44
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Xsolve\AssociateTests\Functional\GenericEntity\Mock;
4
5
use Xsolve\AssociateTests\Functional\DataProviderInterface;
6
use Xsolve\AssociateTests\Functional\GenericEntity\Mock\Model\Category;
7
use Xsolve\AssociateTests\Functional\GenericEntity\Mock\Model\Product;
8
use Xsolve\AssociateTests\Functional\GenericEntity\Mock\Model\Store;
9
use Xsolve\AssociateTests\Functional\GenericEntity\Mock\Model\User;
10
use Xsolve\AssociateTests\Functional\GenericEntity\Mock\Model\Variant;
11
use Symfony\Component\Yaml\Yaml;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Yaml\Yaml was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
class DataProvider implements DataProviderInterface
14
{
15
    /**
16
     * @var User[]
17
     */
18
    protected $usersMap = [];
19
20
    /**
21
     * @var Store[]
22
     */
23
    protected $storesMap = [];
24
25
    /**
26
     * @var Product[]
27
     */
28
    protected $productsMap = [];
29
30
    /**
31
     * @var Variant[]
32
     */
33
    protected $variantsMap = [];
34
35
    /**
36
     * @var Category[]
37
     */
38
    protected $categoriesMap = [];
39
40
    public function __construct()
41
    {
42
        $data = Yaml::parse(
43
            file_get_contents(__DIR__ . '/../../data.yml')
44
        );
45
46
        $categoriesMap = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $categoriesMap is dead and can be removed.
Loading history...
47
        foreach ($data['categories'] as $categoryData) {
48
            $category = new Category();
49
            $category->setId($categoryData['id']);
50
            $category->setName($categoryData['name']);
51
            $this->categoriesMap[$category->getId()] = $category;
52
        }
53
54
        foreach ($data['users'] as $userData) {
55
            $user = new User();
56
            $user->setId($userData['id']);
57
            foreach ($userData['stores'] as $storeData) {
58
                $store = new Store();
59
                $store->setUser($user);
60
                $user->addStore($store);
61
                $store->setId($storeData['id']);
62
                $store->setApproved($storeData['approved']);
63
                foreach ($storeData['products'] as $productData) {
64
                    $product = new Product();
65
                    $product->setStore($store);
66
                    $store->addProduct($product);
67
                    $product->setId($productData['id']);
68
                    $product->setApproved($productData['approved']);
69
                    $product->setName($productData['name']);
70
                    $product->setCategory($this->categoriesMap[$productData['categoryId']]);
71
                    foreach ($productData['variants'] as $variantData) {
72
                        $variant = new Variant();
73
                        $variant->setProduct($product);
74
                        $product->addVariant($variant);
75
                        $variant->setId($variantData['id']);
76
                        $variant->setPrice($variantData['price']);
77
                        $this->variantsMap[$variant->getId()] = $variant;
78
                    }
79
                    $this->productsMap[$product->getId()] = $product;
80
                }
81
                $this->storesMap[$store->getId()] = $store;
82
            }
83
            $this->usersMap[$user->getId()] = $user;
84
        }
85
    }
86
87
    /**
88
     * @return User[]
89
     */
90
    public function getUsers(array $ids = null): array
91
    {
92
        if (is_array($ids)) {
93
            return array_values(
94
                array_intersect_key(
95
                    $this->usersMap,
96
                    array_flip($ids)
97
                )
98
            );
99
        }
100
101
        return array_values($this->usersMap);
102
    }
103
104
    /**
105
     * @return Store[]
106
     */
107
    public function getStores(array $ids = null): array
108
    {
109
        if (is_array($ids)) {
110
            return array_values(
111
                array_intersect_key(
112
                    $this->storesMap,
113
                    array_flip($ids)
114
                )
115
            );
116
        }
117
118
        return array_values($this->storesMap);
119
    }
120
121
    /**
122
     * @return Product[]
123
     */
124
    public function getProducts(array $ids = null): array
125
    {
126
        if (is_array($ids)) {
127
            return array_values(
128
                array_intersect_key(
129
                    $this->productsMap,
130
                    array_flip($ids)
131
                )
132
            );
133
        }
134
135
        return array_values($this->productsMap);
136
    }
137
138
    /**
139
     * @return Variant[]
140
     */
141
    public function getVariants(array $ids = null): array
142
    {
143
        if (is_array($ids)) {
144
            return array_values(
145
                array_intersect_key(
146
                    $this->variantsMap,
147
                    array_flip($ids)
148
                )
149
            );
150
        }
151
152
        return array_values($this->variantsMap);
153
    }
154
155
    /**
156
     * @return Category[]
157
     */
158
    public function getCategories(array $ids = null): array
159
    {
160
        if (is_array($ids)) {
161
            return array_values(
162
                array_intersect_key(
163
                    $this->categoriesMap,
164
                    array_flip($ids)
165
                )
166
            );
167
        }
168
169
        return array_values($this->categoriesMap);
170
    }
171
172
    /**
173
     * @return array
174
     */
175
    public function getAll(): array
176
    {
177
        return array_merge(
178
            $this->getCategories(),
179
            $this->getUsers(),
180
            $this->getStores(),
181
            $this->getProducts(),
182
            $this->getVariants()
183
        );
184
    }
185
}
186