Completed
Push — master ( 97fb46...770b11 )
by Gaetano
15:34 queued 07:06
created

ObjectStateGroupManager   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Test Coverage

Coverage 82.73%

Importance

Changes 0
Metric Value
eloc 114
dl 0
loc 257
ccs 91
cts 110
cp 0.8273
rs 9.36
c 0
b 0
f 0
wmc 38

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getReferencesValues() 0 22 6
A load() 0 7 1
A create() 0 28 6
A matchObjectStateGroups() 0 10 2
A delete() 0 13 2
B generateMigration() 0 71 8
A listAllowedConditions() 0 3 1
B update() 0 36 11
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup;
6
use Kaliop\eZMigrationBundle\API\Collection\ObjectStateGroupCollection;
7
use Kaliop\eZMigrationBundle\Core\Matcher\ObjectStateGroupMatcher;
8
use Kaliop\eZMigrationBundle\API\MigrationGeneratorInterface;
9
use Kaliop\eZMigrationBundle\API\EnumerableMatcherInterface;
10
11
/**
12
 * Handles object-state-group migrations.
13
 */
14
class ObjectStateGroupManager extends RepositoryExecutor implements MigrationGeneratorInterface, EnumerableMatcherInterface
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $supportedStepTypes = array('object_state_group');
20
    protected $supportedActions = array('create', 'load', 'update', 'delete');
21
22
    /**
23
     * @var ObjectStateGroupMatcher
24
     */
25
    protected $objectStateGroupMatcher;
26
27
    /**
28
     * @param ObjectStateGroupMatcher $objectStateGroupMatcher
29
     */
30 96
    public function __construct(ObjectStateGroupMatcher $objectStateGroupMatcher)
31
    {
32 96
        $this->objectStateGroupMatcher = $objectStateGroupMatcher;
33 96
    }
34
35
    /**
36
     * Handles the create step of object state group migrations
37
     */
38 1
    protected function create($step)
39
    {
40 1
        foreach (array('names', 'identifier') as $key) {
41 1
            if (!isset($step->dsl[$key])) {
42 1
                throw new \Exception("The '$key' key is missing in a object state group creation definition");
43
            }
44
        }
45
46 1
        $objectStateService = $this->repository->getObjectStateService();
47
48 1
        $objectStateGroupIdentifier = $this->referenceResolver->resolveReference($step->dsl['identifier']);
49 1
        $objectStateGroupCreateStruct = $objectStateService->newObjectStateGroupCreateStruct($objectStateGroupIdentifier);
50 1
        $objectStateGroupCreateStruct->defaultLanguageCode = $this->getLanguageCode($step); // was: self::DEFAULT_LANGUAGE_CODE;
51
52 1
        foreach ($step->dsl['names'] as $languageCode => $name) {
53 1
            $objectStateGroupCreateStruct->names[$languageCode] = $name;
54
        }
55 1
        if (isset($step->dsl['descriptions'])) {
56 1
            foreach ($step->dsl['descriptions'] as $languageCode => $description) {
57 1
                $objectStateGroupCreateStruct->descriptions[$languageCode] = $description;
58
            }
59
        }
60
61 1
        $objectStateGroup = $objectStateService->createObjectStateGroup($objectStateGroupCreateStruct);
62
63 1
        $this->setReferences($objectStateGroup, $step);
64
65 1
        return $objectStateGroup;
66
    }
67
68
    protected function load($step)
69
    {
70
        $groupsCollection = $this->matchObjectStateGroups('load', $step);
71
72
        $this->setReferences($groupsCollection, $step);
73
74
        return $groupsCollection;
75
    }
76
77
    /**
78
     * Handles the update step of object state group migrations
79
     *
80
     * @todo add support for defaultLanguageCode
81
     */
82 1
    protected function update($step)
83
    {
84 1
        $objectStateService = $this->repository->getObjectStateService();
85
86 1
        $groupsCollection = $this->matchObjectStateGroups('update', $step);
87
88 1
        if (count($groupsCollection) > 1 && isset($step->dsl['references'])) {
89
            throw new \Exception("Can not execute Object State Group update because multiple groups match, and a references section is specified in the dsl. References can be set when only 1 state group matches");
90
        }
91
92 1
        if (count($groupsCollection) > 1 && isset($step->dsl['identifier'])) {
93
            throw new \Exception("Can not execute Object State Group update because multiple groups match, and an identifier is specified in the dsl.");
94
        }
95
96 1
        foreach ($groupsCollection as $objectStateGroup) {
97 1
            $objectStateGroupUpdateStruct = $objectStateService->newObjectStateGroupUpdateStruct();
98
99 1
            if (isset($step->dsl['identifier'])) {
100 1
                $objectStateGroupUpdateStruct->identifier = $this->referenceResolver->resolveReference($step->dsl['identifier']);
101
            }
102 1
            if (isset($step->dsl['names'])) {
103
                foreach ($step->dsl['names'] as $languageCode => $name) {
104
                    $objectStateGroupUpdateStruct->names[$languageCode] = $name;
105
                }
106
            }
107 1
            if (isset($step->dsl['descriptions'])) {
108
                foreach ($step->dsl['descriptions'] as $languageCode => $description) {
109
                    $objectStateGroupUpdateStruct->descriptions[$languageCode] = $description;
110
                }
111
            }
112 1
            $objectStateGroup = $objectStateService->updateObjectStateGroup($objectStateGroup, $objectStateGroupUpdateStruct);
113
114 1
            $this->setReferences($objectStateGroup, $step);
115
        }
116
117 1
        return $groupsCollection;
118
    }
119
120
    /**
121
     * Handles the delete step of object state group migrations
122
     */
123 1
    protected function delete($step)
124
    {
125 1
        $groupsCollection = $this->matchObjectStateGroups('delete', $step);
126
127 1
        $this->setReferences($groupsCollection, $step);
128
129 1
        $objectStateService = $this->repository->getObjectStateService();
130
131 1
        foreach ($groupsCollection as $objectStateGroup) {
132 1
            $objectStateService->deleteObjectStateGroup($objectStateGroup);
133
        }
134
135 1
        return $groupsCollection;
136
    }
137
138
    /**
139
     * @param string $action
140
     * @return ObjectStateGroupCollection
141
     * @throws \Exception
142
     */
143 1
    protected function matchObjectStateGroups($action, $step)
144
    {
145 1
        if (!isset($step->dsl['match'])) {
146
            throw new \Exception("A match condition is required to $action an object state group");
147
        }
148
149
        // convert the references passed in the match
150 1
        $match = $this->resolveReferencesRecursively($step->dsl['match']);
151
152 1
        return $this->objectStateGroupMatcher->match($match);
153
    }
154
155
    /**
156
     * @param ObjectStateGroup $objectStateGroup
157
     * @param array $references the definitions of the references to set
158
     * @throws \InvalidArgumentException When trying to assign a reference to an unsupported attribute
159
     * @return array key: the reference names, values: the reference values
160
     */
161 1
    protected function getReferencesValues($objectStateGroup, array $references, $step)
162
    {
163 1
        $refs = array();
164
165 1
        foreach ($references as $reference) {
166 1
            switch ($reference['attribute']) {
167 1
                case 'object_state_group_id':
168 1
                case 'id':
169 1
                    $value = $objectStateGroup->id;
170 1
                    break;
171
                case 'object_state_group_identifier':
172
                case 'identifier':
173
                    $value = $objectStateGroup->id;
174
                    break;
175
                default:
176
                    throw new \InvalidArgumentException('Object State Group Manager does not support setting references for attribute ' . $reference['attribute']);
177
            }
178
179 1
            $refs[$reference['identifier']] = $value;
180
        }
181
182 1
        return $refs;
183
    }
184
185
    /**
186
     * @param array $matchCondition
187
     * @param string $mode
188
     * @param array $context
189
     * @throws \Exception
190
     * @return array
191
     */
192 3
    public function generateMigration(array $matchCondition, $mode, array $context = array())
193
    {
194 3
        $previousUserId = $this->loginUser($this->getAdminUserIdentifierFromContext($context));
195 3
        $objectStateGroupCollection = $this->objectStateGroupMatcher->match($matchCondition);
196 3
        $data = array();
197
198
        /** @var \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup */
199 3
        foreach ($objectStateGroupCollection as $objectStateGroup) {
200
201
            $groupData = array(
202 3
                'type' => reset($this->supportedStepTypes),
203 3
                'mode' => $mode,
204
            );
205
206
            switch ($mode) {
207 3
                case 'create':
208 1
                    $groupData = array_merge(
209 1
                        $groupData,
210
                        array(
211 1
                            'identifier' => $objectStateGroup->identifier,
212
                        )
213
                    );
214 1
                    break;
215 2
                case 'update':
216 1
                    $groupData = array_merge(
217 1
                        $groupData,
218
                        array(
219
                            'match' => array(
220 1
                                ObjectStateGroupMatcher::MATCH_OBJECTSTATEGROUP_IDENTIFIER => $objectStateGroup->identifier
221
                            ),
222 1
                            'identifier' => $objectStateGroup->identifier,
223
                        )
224
                    );
225 1
                    break;
226 1
                case 'delete':
227 1
                    $groupData = array_merge(
228 1
                        $groupData,
229
                        array(
230
                            'match' => array(
231 1
                                ObjectStateGroupMatcher::MATCH_OBJECTSTATEGROUP_IDENTIFIER => $objectStateGroup->identifier
232
                            )
233
                        )
234
                    );
235 1
                    break;
236
                default:
237
                    throw new \Exception("Executor 'object_state_group' doesn't support mode '$mode'");
238
            }
239
240 3
            if ($mode != 'delete') {
241 2
                $names = array();
242 2
                $descriptions = array();
243 2
                foreach($objectStateGroup->languageCodes as $languageCode) {
244 2
                    $names[$languageCode] =  $objectStateGroup->getName($languageCode);
245
                }
246 2
                foreach($objectStateGroup->languageCodes as $languageCode) {
247 2
                    $descriptions[$languageCode] =  $objectStateGroup->getDescription($languageCode);
248
                }
249 2
                $groupData = array_merge(
250 2
                    $groupData,
251
                    array(
252 2
                        'names' => $names,
253 2
                        'descriptions' => $descriptions,
254
                    )
255
                );
256
            }
257
258 3
            $data[] = $groupData;
259
        }
260
261 3
        $this->loginUser($previousUserId);
262 3
        return $data;
263
    }
264
265
    /**
266
     * @return string[]
267
     */
268
    public function listAllowedConditions()
269
    {
270
        return $this->objectStateGroupMatcher->listAllowedConditions();
271
    }
272
}
273