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