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

SectionManager::generateMigration()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 55
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 5.001

Importance

Changes 0
Metric Value
cc 5
eloc 36
nc 5
nop 3
dl 0
loc 55
ccs 28
cts 29
cp 0.9655
crap 5.001
rs 9.0328
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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