1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Assembler\CategoryAssembler |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Assembler; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\MemberNames; |
24
|
|
|
use TechDivision\Import\Serializer\SerializerInterface; |
25
|
|
|
use TechDivision\Import\Repositories\CategoryRepository; |
26
|
|
|
use TechDivision\Import\Repositories\CategoryVarcharRepository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Repository implementation to assemble category data. |
30
|
|
|
* |
31
|
|
|
* @author Tim Wagner <[email protected]> |
32
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
34
|
|
|
* @link https://github.com/techdivision/import |
35
|
|
|
* @link http://www.techdivision.com |
36
|
|
|
*/ |
37
|
|
|
class CategoryAssembler implements CategoryAssemblerInterface |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The serialize instance. |
42
|
|
|
* |
43
|
|
|
* @var \TechDivision\Import\Serializer\SerializerInterface |
44
|
|
|
*/ |
45
|
|
|
protected $serializer; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The repository to access categories. |
49
|
|
|
* |
50
|
|
|
* @var \TechDivision\Import\Repositories\CategoryRepository |
51
|
|
|
*/ |
52
|
|
|
protected $categoryRepository; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The repository to access category varchar values. |
56
|
|
|
* |
57
|
|
|
* @var \TechDivision\Import\Repositories\CategoryVarcharRepository |
58
|
|
|
*/ |
59
|
|
|
protected $categoryVarcharRepository; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Initialize the assembler with the passed instances. |
63
|
|
|
* |
64
|
|
|
* @param \TechDivision\Import\Repositories\CategoryRepository $categoryRepository The repository to access categories |
65
|
|
|
* @param \TechDivision\Import\Repositories\CategoryVarcharRepository $categoryVarcharRepository The repository instance |
66
|
|
|
* @param \TechDivision\Import\Serializer\SerializerInterface $serializer The serializer instance |
67
|
|
|
*/ |
68
|
|
|
public function __construct( |
69
|
|
|
CategoryRepository $categoryRepository, |
70
|
|
|
CategoryVarcharRepository $categoryVarcharRepository, |
71
|
|
|
SerializerInterface $serializer |
72
|
|
|
) { |
73
|
|
|
$this->categoryRepository = $categoryRepository; |
74
|
|
|
$this->categoryVarcharRepository = $categoryVarcharRepository; |
75
|
|
|
$this->serializer = $serializer; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns an array with the available categories and their |
80
|
|
|
* resolved path as keys. |
81
|
|
|
* |
82
|
|
|
* @return array The array with the categories |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function getCategoriesWithResolvedPath() |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
|
87
|
|
|
// prepare the categories |
88
|
|
|
$categories = array(); |
89
|
|
|
|
90
|
|
|
// load the categories from the database |
91
|
|
|
$availableCategories = $this->categoryRepository->findAll(); |
92
|
|
|
|
93
|
|
|
// create the array with the resolved category path as keys |
94
|
|
|
foreach ($availableCategories as $category) { |
95
|
|
|
// expload the entity IDs from the category path |
96
|
|
|
$entityIds = $this->serializer->explode($category[MemberNames::PATH]); |
97
|
|
|
|
98
|
|
|
// cut-off the root category |
99
|
|
|
array_shift($entityIds); |
100
|
|
|
|
101
|
|
|
// continue with the next category if no entity IDs are available |
102
|
|
|
if (sizeof($entityIds) === 0) { |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// initialize the array for the path elements |
107
|
|
|
$path = array(); |
108
|
|
|
foreach ($entityIds as $entityId) { |
109
|
|
|
$cat = $this->categoryVarcharRepository->findByEntityId($entityId); |
110
|
|
|
if ($cat && $cat[MemberNames::VALUE]) { |
|
|
|
|
111
|
|
|
$path[] = $cat[MemberNames::VALUE]; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// append the catogory with the string path as key |
116
|
|
|
$categories[$this->serializer->implode($path)] = $category; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// return array with the categories |
120
|
|
|
return $categories; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Return's an array with the available categories and their resolved path |
126
|
|
|
* as keys by store view ID. |
127
|
|
|
* |
128
|
|
|
* @param integer $storeViewId The store view ID to return the categories for |
129
|
|
|
* |
130
|
|
|
* @return array The available categories for the passed store view ID |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
public function getCategoriesWithResolvedPathByStoreView($storeViewId) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
// prepare the categories |
135
|
|
|
$categories = array(); |
136
|
|
|
|
137
|
|
|
// load the categories from the database |
138
|
|
|
$availableCategories = $this->categoryRepository->findAllByStoreView($storeViewId); |
139
|
|
|
|
140
|
|
|
// create the array with the resolved category path as keys |
141
|
|
|
foreach ($availableCategories as $category) { |
142
|
|
|
// expload the entity IDs from the category path |
143
|
|
|
$entityIds = $this->serializer->explode($category[MemberNames::PATH]); |
144
|
|
|
|
145
|
|
|
// cut-off the root category |
146
|
|
|
array_shift($entityIds); |
147
|
|
|
|
148
|
|
|
// continue with the next category if no entity IDs are available |
149
|
|
|
if (sizeof($entityIds) === 0) { |
150
|
|
|
continue; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// initialize the array for the path elements |
154
|
|
|
$path = array(); |
155
|
|
|
foreach ($entityIds as $entityId) { |
156
|
|
|
$cat = $this->categoryVarcharRepository->findByEntityId($entityId); |
157
|
|
|
if ($cat && $cat[MemberNames::VALUE]) { |
|
|
|
|
158
|
|
|
$path[] = $cat[MemberNames::VALUE]; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// append the catogory with the string path as key |
163
|
|
|
$categories[$this->serializer->implode($path)] = $category; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// return array with the categories |
167
|
|
|
return $categories; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.