Completed
Push — master ( 4484df...ecad36 )
by Gaetano
07:23
created

SectionManager::getReferencesValues()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 27
Code Lines 20

Duplication

Lines 27
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 27
loc 27
c 0
b 0
f 0
ccs 0
cts 24
cp 0
rs 5.3846
cc 8
eloc 20
nc 8
nop 2
crap 72
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\Core\Matcher\SectionMatcher;
9
10
/**
11
 * Handles section migrations.
12
 */
13
class SectionManager extends RepositoryExecutor implements MigrationGeneratorInterface
14
{
15
    protected $supportedStepTypes = array('section');
16
17
    /** @var SectionMatcher $sectionMatcher */
18
    protected $sectionMatcher;
19
20
    /**
21
     * @param SectionMatcher $sectionMatcher
22
     */
23
    public function __construct(SectionMatcher $sectionMatcher)
24
    {
25
        $this->sectionMatcher = $sectionMatcher;
26
    }
27
28
    /**
29
     * Handles the section create migration action
30
     */
31
    protected function create($step)
32
    {
33 View Code Duplication
        foreach (array('name', 'identifier') as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
34
            if (!isset($step->dsl[$key])) {
35
                throw new \Exception("The '$key' key is missing in a section creation definition");
36
            }
37
        }
38
39
        $sectionService = $this->repository->getSectionService();
40
41
        $sectionCreateStruct = $sectionService->newSectionCreateStruct();
42
43
        $sectionIdentifier = $this->referenceResolver->resolveReference($step->dsl['identifier']);
44
        $sectionCreateStruct->identifier = $sectionIdentifier;
45
        $sectionCreateStruct->name = $step->dsl['name'];
46
47
        $section = $sectionService->createSection($sectionCreateStruct);
48
49
        $this->setReferences($section, $step);
0 ignored issues
show
Documentation introduced by
$section is of type object<eZ\Publish\API\Re...Values\Content\Section>, but the function expects a object<Object>|object<Ka...ctCollectionCollection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
51
        return $section;
52
    }
53
54
    /**
55
     * Handles the section update migration action
56
     */
57
    protected function update($step)
58
    {
59
        $sectionCollection = $this->matchSections('update', $step);
60
61
        if (count($sectionCollection) > 1 && array_key_exists('references', $step->dsl)) {
62
            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");
63
        }
64
65
        $sectionService = $this->repository->getSectionService();
66
        foreach ($sectionCollection as $key => $section) {
0 ignored issues
show
Bug introduced by
The expression $sectionCollection of type object<Kaliop\eZMigratio...SectionCollection>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
67
            $sectionUpdateStruct = $sectionService->newSectionUpdateStruct();
68
69
            if (isset($step->dsl['identifier'])) {
70
                $sectionUpdateStruct->identifier = $this->referenceResolver->resolveReference($step->dsl['identifier']);
71
            }
72
            if (isset($step->dsl['name'])) {
73
                $sectionUpdateStruct->name = $step->dsl['name'];
74
            }
75
76
            $section = $sectionService->updateSection($section, $sectionUpdateStruct);
77
78
            $sectionCollection[$key] = $section;
79
        }
80
81
        $this->setReferences($sectionCollection, $step);
0 ignored issues
show
Documentation introduced by
$sectionCollection is of type object<Kaliop\eZMigratio...SectionCollection>|null, but the function expects a object<Object>|object<Ka...ctCollectionCollection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
83
        return $sectionCollection;
84
    }
85
86
    /**
87
     * Handles the section delete migration action
88
     */
89
    protected function delete($step)
90
    {
91
        $sectionCollection = $this->matchSections('delete', $step);
92
93
        $this->setReferences($sectionCollection, $step);
0 ignored issues
show
Documentation introduced by
$sectionCollection is of type object<Kaliop\eZMigratio...SectionCollection>|null, but the function expects a object<Object>|object<Ka...ctCollectionCollection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
95
        $sectionService = $this->repository->getSectionService();
96
97
        foreach ($sectionCollection as $section) {
0 ignored issues
show
Bug introduced by
The expression $sectionCollection of type object<Kaliop\eZMigratio...SectionCollection>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
98
            $sectionService->deleteSection($section);
99
        }
100
101
        return $sectionCollection;
102
    }
103
104
    /**
105
     * @param string $action
106
     * @return SectionCollection
107
     * @throws \Exception
108
     */
109 View Code Duplication
    protected function matchSections($action, $step)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
110
    {
111
        if (!isset($step->dsl['match'])) {
112
            throw new \Exception("A match condition is required to $action a section");
113
        }
114
115
        // convert the references passed in the match
116
        $match = $this->resolveReferencesRecursively($step->dsl['match']);
117
118
        return $this->sectionMatcher->match($match);
119
    }
120
121
    /**
122
     * @param Section $section
123
     * @param array $references the definitions of the references to set
124
     * @throws \InvalidArgumentException When trying to assign a reference to an unsupported attribute
125
     * @return array key: the reference names, values: the reference values
126
     */
127 View Code Duplication
    protected function getReferencesValues($section, array $references)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
128
    {
129
        $refs = array();
130
131
        foreach ($references as $reference) {
132
            switch ($reference['attribute']) {
133
                case 'section_id':
134
                case 'id':
135
                    $value = $section->id;
136
                    break;
137
                case 'section_identifier':
138
                case 'identifier':
139
                    $value = $section->identifier;
140
                    break;
141
                case 'section_name':
142
                case 'name':
143
                    $value = $section->name;
144
                    break;
145
                default:
146
                    throw new \InvalidArgumentException('Section Manager does not support setting references for attribute ' . $reference['attribute']);
147
            }
148
149
            $refs[$reference['identifier']] = $value;
150
        }
151
152
        return $refs;
153
    }
154
155
    /**
156
     * @param array $matchCondition
157
     * @param string $mode
158
     * @param array $context
159
     * @throws \Exception
160
     * @return array
161
     */
162 View Code Duplication
    public function generateMigration(array $matchCondition, $mode, array $context = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
163
    {
164
        $previousUserId = $this->loginUser($this->getAdminUserIdentifierFromContext($context));
165
        $sectionCollection = $this->sectionMatcher->match($matchCondition);
166
        $data = array();
167
168
        /** @var \eZ\Publish\API\Repository\Values\Content\Section $section */
169
        foreach ($sectionCollection as $section) {
0 ignored issues
show
Bug introduced by
The expression $sectionCollection of type object<Kaliop\eZMigratio...SectionCollection>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
170
171
            $sectionData = array(
172
                'type' => reset($this->supportedStepTypes),
173
                'mode' => $mode,
174
            );
175
176
            switch ($mode) {
177
                case 'create':
178
                    $sectionData = array_merge(
179
                        $sectionData,
180
                        array(
181
                            'identifier' => $section->identifier,
182
                            'name' => $section->name,
183
                        )
184
                    );
185
                    break;
186
                case 'update':
187
                    $sectionData = array_merge(
188
                        $sectionData,
189
                        array(
190
                            'match' => array(
191
                                SectionMatcher::MATCH_SECTION_ID => $section->id
192
                            ),
193
                            'identifier' => $section->identifier,
194
                            'name' => $section->name,
195
                        )
196
                    );
197
                    break;
198
                case 'delete':
199
                    $sectionData = array_merge(
200
                        $sectionData,
201
                        array(
202
                            'match' => array(
203
                                SectionMatcher::MATCH_SECTION_ID => $section->id
204
                            )
205
                        )
206
                    );
207
                    break;
208
                default:
209
                    throw new \Exception("Executor 'section' doesn't support mode '$mode'");
210
            }
211
212
            $data[] = $sectionData;
213
        }
214
215
        $this->loginUser($previousUserId);
216
        return $data;
217
    }
218
}
219