1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Category\Plugins\ChildrenCountPlugin |
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-category |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Category\Plugins; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\EntityStatus; |
24
|
|
|
use TechDivision\Import\Plugins\AbstractPlugin; |
25
|
|
|
use TechDivision\Import\Category\Utils\MemberNames; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Plugin that updates the categories children count attribute after a successfull category import. |
29
|
|
|
* |
30
|
|
|
* @author Tim Wagner <[email protected]> |
31
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
33
|
|
|
* @link https://github.com/techdivision/import-category |
34
|
|
|
* @link http://www.techdivision.com |
35
|
|
|
*/ |
36
|
|
|
class ChildrenCountPlugin extends AbstractPlugin |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Process the plugin functionality. |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
* @throws \Exception Is thrown, if the plugin can not be processed |
44
|
|
|
*/ |
45
|
|
|
public function process() |
46
|
|
|
{ |
47
|
|
|
|
48
|
|
|
// load all available categories |
49
|
|
|
$categories = $this->getImportProcessor()->getCategories(); |
50
|
|
|
|
51
|
|
|
// update the categories children count |
52
|
|
|
foreach ($categories as $category) { |
53
|
|
|
// load the category itself |
54
|
|
|
$this->category = $this->loadCategory($pk = $this->getPrimaryKey($category)); |
|
|
|
|
55
|
|
|
|
56
|
|
|
// update the category's children count |
57
|
|
|
$this->persistCategory($this->initializeCategory($this->prepareAttributes())); |
58
|
|
|
|
59
|
|
|
// write a log message |
60
|
|
|
$this->getSystemLogger()->debug( |
61
|
|
|
sprintf('Successfully updated category with primary key %d', $pk) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
68
|
|
|
* |
69
|
|
|
* @return array The prepared attributes |
70
|
|
|
*/ |
71
|
|
|
protected function prepareAttributes() |
72
|
|
|
{ |
73
|
|
|
|
74
|
|
|
// initialize the values that has to be updated |
75
|
|
|
$updatedAt = date('Y-m-d H:i:s'); |
76
|
|
|
$childrenCount = $this->loadCategoryChildrenChildrenCount($this->category[MemberNames::PATH] . '/%'); |
77
|
|
|
|
78
|
|
|
// prepare and return the array with the updated values |
79
|
|
|
return array( |
80
|
|
|
MemberNames::UPDATED_AT => $updatedAt, |
81
|
|
|
MemberNames::CHILDREN_COUNT => $childrenCount |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Initialize the category with the passed attributes and returns an instance. |
87
|
|
|
* |
88
|
|
|
* @param array $attr The category attributes |
89
|
|
|
* |
90
|
|
|
* @return array The initialized category |
91
|
|
|
*/ |
92
|
|
|
protected function initializeCategory(array $attr) |
93
|
|
|
{ |
94
|
|
|
return $this->mergeEntity( |
95
|
|
|
$this->category, |
96
|
|
|
$attr |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return's the primary key of the passed category. |
102
|
|
|
* |
103
|
|
|
* @param array $category The category to return the primary key for |
104
|
|
|
* |
105
|
|
|
* @return integer The primary key of the category |
106
|
|
|
*/ |
107
|
|
|
protected function getPrimaryKey(array $category) |
108
|
|
|
{ |
109
|
|
|
return $category[MemberNames::ENTITY_ID]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Return's the category with the passed ID. |
114
|
|
|
* |
115
|
|
|
* @param string $id The ID of the category to return |
116
|
|
|
* |
117
|
|
|
* @return array The category |
118
|
|
|
*/ |
119
|
|
|
protected function loadCategory($id) |
120
|
|
|
{ |
121
|
|
|
return $this->getProcessor()->loadCategory($id); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Return's the children count of the category with the passed path. |
126
|
|
|
* |
127
|
|
|
* @param string $path The path of the category to count the children for |
128
|
|
|
* |
129
|
|
|
* @return integer The children count of the category with the passed path |
130
|
|
|
*/ |
131
|
|
|
protected function loadCategoryChildrenChildrenCount($path) |
132
|
|
|
{ |
133
|
|
|
return $this->getProcessor()->loadCategoryChildrenChildrenCount($path); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Persist's the passed category data and return's the ID. |
138
|
|
|
* |
139
|
|
|
* @param array $category The category data to persist |
140
|
|
|
* |
141
|
|
|
* @return string The ID of the persisted entity |
142
|
|
|
*/ |
143
|
|
|
protected function persistCategory($category) |
144
|
|
|
{ |
145
|
|
|
return $this->getProcessor()->persistCategory($category); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Merge's and return's the entity with the passed attributes and set's the |
150
|
|
|
* status to 'update'. |
151
|
|
|
* |
152
|
|
|
* @param array $entity The entity to merge the attributes into |
153
|
|
|
* @param array $attr The attributes to be merged |
154
|
|
|
* |
155
|
|
|
* @return array The merged entity |
156
|
|
|
*/ |
157
|
|
|
protected function mergeEntity(array $entity, array $attr) |
158
|
|
|
{ |
159
|
|
|
return array_merge($entity, $attr, array(EntityStatus::MEMBER_NAME => EntityStatus::STATUS_UPDATE)); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: