forked from kaliop-uk/ezmigrationbundle
/
SectionManager.php
227 lines (188 loc) · 7.4 KB
/
SectionManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
namespace Kaliop\eZMigrationBundle\Core\Executor;
use Kaliop\eZMigrationBundle\API\Collection\SectionCollection;
use Kaliop\eZMigrationBundle\API\MigrationGeneratorInterface;
use Kaliop\eZMigrationBundle\Core\Matcher\SectionMatcher;
/**
* Handles section migrations.
*/
class SectionManager extends RepositoryExecutor implements MigrationGeneratorInterface
{
protected $supportedStepTypes = array('section');
/** @var SectionMatcher $sectionMatcher */
protected $sectionMatcher;
/**
* @param SectionMatcher $sectionMatcher
*/
public function __construct(SectionMatcher $sectionMatcher)
{
$this->sectionMatcher = $sectionMatcher;
}
/**
* Handles the section create migration action
*/
protected function create($step)
{
foreach (array('name', 'identifier') as $key) {
if (!isset($step->dsl[$key])) {
throw new \Exception("The '$key' key is missing in a section creation definition");
}
}
$sectionService = $this->repository->getSectionService();
$sectionCreateStruct = $sectionService->newSectionCreateStruct();
$sectionIdentifier = $this->referenceResolver->resolveReference($step->dsl['identifier']);
$sectionCreateStruct->identifier = $sectionIdentifier;
$sectionCreateStruct->name = $step->dsl['name'];
$section = $sectionService->createSection($sectionCreateStruct);
$this->setReferences($section, $step);
return $section;
}
/**
* Handles the section update migration action
*/
protected function update($step)
{
$sectionCollection = $this->matchSections('update', $step);
if (count($sectionCollection) > 1 && array_key_exists('references', $step->dsl)) {
throw new \Exception("Can not execute Section update because multiple types match, and a references section is specified in the dsl. References can be set when only 1 section matches");
}
$sectionService = $this->repository->getSectionService();
foreach ($sectionCollection as $key => $section) {
$sectionUpdateStruct = $sectionService->newSectionUpdateStruct();
if (isset($step->dsl['identifier'])) {
$sectionUpdateStruct->identifier = $this->referenceResolver->resolveReference($step->dsl['identifier']);
}
if (isset($step->dsl['name'])) {
$sectionUpdateStruct->name = $step->dsl['name'];
}
$section = $sectionService->updateSection($section, $sectionUpdateStruct);
$sectionCollection[$key] = $section;
}
$this->setReferences($sectionCollection, $step);
return $sectionCollection;
}
/**
* Handles the section delete migration action
*/
protected function delete($step)
{
$sectionCollection = $this->matchSections('delete', $step);
$this->setReferences($sectionCollection, $step);
$sectionService = $this->repository->getSectionService();
foreach ($sectionCollection as $section) {
$sectionService->deleteSection($section);
}
return $sectionCollection;
}
/**
* @param string $action
* @return SectionCollection
* @throws \Exception
*/
protected function matchSections($action, $step)
{
if (!isset($step->dsl['match'])) {
throw new \Exception("A match condition is required to $action a section");
}
// convert the references passed in the match
$match = $this->resolveReferencesRecursively($step->dsl['match']);
return $this->sectionMatcher->match($match);
}
/**
* Sets references to certain section attributes.
*
* @param \eZ\Publish\API\Repository\Values\Content\Section|SectionCollection $section
* @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute
* @return boolean
*/
protected function setReferences($section, $step)
{
if (!array_key_exists('references', $step->dsl)) {
return false;
}
$references = $this->setReferencesCommon($section, $step->dsl['references']);
$section = $this->insureSingleEntity($section, $references);
foreach ($references as $reference) {
switch ($reference['attribute']) {
case 'section_id':
case 'id':
$value = $section->id;
break;
case 'section_identifier':
case 'identifier':
$value = $section->identifier;
break;
case 'section_name':
case 'name':
$value = $section->name;
break;
default:
throw new \InvalidArgumentException('Section Manager does not support setting references for attribute ' . $reference['attribute']);
}
$overwrite = false;
if (isset($reference['overwrite'])) {
$overwrite = $reference['overwrite'];
}
$this->referenceResolver->addReference($reference['identifier'], $value, $overwrite);
}
return true;
}
/**
* @param array $matchCondition
* @param string $mode
* @param array $context
* @throws \Exception
* @return array
*/
public function generateMigration(array $matchCondition, $mode, array $context = array())
{
$previousUserId = $this->loginUser($this->getAdminUserIdentifierFromContext($context));
$sectionCollection = $this->sectionMatcher->match($matchCondition);
$data = array();
/** @var \eZ\Publish\API\Repository\Values\Content\Section $section */
foreach ($sectionCollection as $section) {
$sectionData = array(
'type' => reset($this->supportedStepTypes),
'mode' => $mode,
);
switch ($mode) {
case 'create':
$sectionData = array_merge(
$sectionData,
array(
'identifier' => $section->identifier,
'name' => $section->name,
)
);
break;
case 'update':
$sectionData = array_merge(
$sectionData,
array(
'match' => array(
SectionMatcher::MATCH_SECTION_ID => $section->id
),
'identifier' => $section->identifier,
'name' => $section->name,
)
);
break;
case 'delete':
$sectionData = array_merge(
$sectionData,
array(
'match' => array(
SectionMatcher::MATCH_SECTION_ID => $section->id
)
)
);
break;
default:
throw new \Exception("Executor 'section' doesn't support mode '$mode'");
}
$data[] = $sectionData;
}
$this->loginUser($previousUserId);
return $data;
}
}