Completed
Pull Request — master (#10)
by Mariusz
03:23
created

DataProvider::getAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Xsolve\LegacyAssociateTests\Functional\DoctrineOrm\Mock;
4
5
use Xsolve\LegacyAssociateTests\Functional\DataProviderInterface;
6
use Xsolve\LegacyAssociateTests\Functional\DoctrineOrm\Mock\Entity\Category;
7
use Xsolve\LegacyAssociateTests\Functional\DoctrineOrm\Mock\Entity\Product;
8
use Xsolve\LegacyAssociateTests\Functional\DoctrineOrm\Mock\Entity\Store;
9
use Xsolve\LegacyAssociateTests\Functional\DoctrineOrm\Mock\Entity\User;
10
use Xsolve\LegacyAssociateTests\Functional\DoctrineOrm\Mock\Entity\Variant;
11
use Symfony\Component\Yaml\Yaml;
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($categoryData['id']);
49
            $category->setName($categoryData['name']);
50
            $this->categoriesMap[$category->getId()] = $category;
51
        }
52
53
        foreach ($data['users'] as $userData) {
54
            $user = new User($userData['id']);
55
            foreach ($userData['stores'] as $storeData) {
56
                $store = new Store($storeData['id']);
57
                $store->setUser($user);
58
                $user->addStore($store);
59
                $store->setApproved($storeData['approved']);
60
                foreach ($storeData['products'] as $productData) {
61
                    $product = new Product($productData['id']);
62
                    $product->setStore($store);
63
                    $store->addProduct($product);
64
                    $product->setApproved($productData['approved']);
65
                    $product->setName($productData['name']);
66
                    $product->setCategory($this->categoriesMap[$productData['categoryId']]);
67
                    foreach ($productData['variants'] as $variantData) {
68
                        $variant = new Variant($variantData['id']);
69
                        $variant->setProduct($product);
70
                        $product->addVariant($variant);
71
                        $variant->setPrice($variantData['price']);
72
                        $this->variantsMap[$variant->getId()] = $variant;
73
                    }
74
                    $this->productsMap[$product->getId()] = $product;
75
                }
76
                $this->storesMap[$store->getId()] = $store;
77
            }
78
            $this->usersMap[$user->getId()] = $user;
79
        }
80
    }
81
82
    /**
83
     * @return User[]
84
     */
85
    public function getUsers(array $ids = null): array
86
    {
87
        if (is_array($ids)) {
88
            return array_values(
89
                array_intersect_key(
90
                    $this->usersMap,
91
                    array_flip($ids)
92
                )
93
            );
94
        }
95
96
        return array_values($this->usersMap);
97
    }
98
99
    /**
100
     * @return Store[]
101
     */
102
    public function getStores(array $ids = null): array
103
    {
104
        if (is_array($ids)) {
105
            return array_values(
106
                array_intersect_key(
107
                    $this->storesMap,
108
                    array_flip($ids)
109
                )
110
            );
111
        }
112
113
        return array_values($this->storesMap);
114
    }
115
116
    /**
117
     * @return Product[]
118
     */
119
    public function getProducts(array $ids = null): array
120
    {
121
        if (is_array($ids)) {
122
            return array_values(
123
                array_intersect_key(
124
                    $this->productsMap,
125
                    array_flip($ids)
126
                )
127
            );
128
        }
129
130
        return array_values($this->productsMap);
131
    }
132
133
    /**
134
     * @return Variant[]
135
     */
136
    public function getVariants(array $ids = null): array
137
    {
138
        if (is_array($ids)) {
139
            return array_values(
140
                array_intersect_key(
141
                    $this->variantsMap,
142
                    array_flip($ids)
143
                )
144
            );
145
        }
146
147
        return array_values($this->variantsMap);
148
    }
149
150
    /**
151
     * @return Category[]
152
     */
153
    public function getCategories(array $ids = null): array
154
    {
155
        if (is_array($ids)) {
156
            return array_values(
157
                array_intersect_key(
158
                    $this->categoriesMap,
159
                    array_flip($ids)
160
                )
161
            );
162
        }
163
164
        return array_values($this->categoriesMap);
165
    }
166
167
    /**
168
     * @return array
169
     */
170
    public function getAll(): array
171
    {
172
        return array_merge(
173
            $this->getCategories(),
174
            $this->getUsers(),
175
            $this->getStores(),
176
            $this->getProducts(),
177
            $this->getVariants()
178
        );
179
    }
180
}
181