Completed
Push — master ( 397eee...f113dc )
by Gaetano
22:13 queued 10:37
created

ContentVersionManager   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 149
Duplicated Lines 35.57 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 18.02%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 6
dl 53
loc 149
ccs 11
cts 61
cp 0.1802
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 22 22 1
A load() 0 8 1
A delete() 19 19 3
C matchVersions() 12 38 13
A getReferencesValues() 0 22 4
A versionStatusToHash() 0 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
6
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
7
use Kaliop\eZMigrationBundle\Core\Matcher\ContentVersionMatcher;
8
use Kaliop\eZMigrationBundle\Core\FieldHandlerManager;
9
use Kaliop\eZMigrationBundle\Core\Matcher\ContentMatcher;
10
use Kaliop\eZMigrationBundle\Core\Matcher\SectionMatcher;
11
use Kaliop\eZMigrationBundle\Core\Matcher\UserMatcher;
12
use Kaliop\eZMigrationBundle\Core\Matcher\ObjectStateMatcher;
13
use Kaliop\eZMigrationBundle\Core\Matcher\ObjectStateGroupMatcher;
14
use Kaliop\eZMigrationBundle\Core\Helper\SortConverter;
15
16
/**
17
 * Handles content-version migrations.
18
 * @todo disallow calling (throw): create, update, generateMigration
19
 */
20
class ContentVersionManager extends ContentManager
21
{
22
    protected $supportedStepTypes = array('content_version');
23
    protected $supportedActions = array('delete', 'load');
24
25
    protected $versionMatcher;
26
27 80 View Code Duplication
    public function __construct(
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...
28
        ContentMatcher $contentMatcher,
29
        SectionMatcher $sectionMatcher,
30
        UserMatcher $userMatcher,
31
        ObjectStateMatcher $objectStateMatcher,
32
        ObjectStateGroupMatcher $objectStateGroupMatcher,
33
        FieldHandlerManager $fieldHandlerManager,
34
        LocationManager $locationManager,
35
        SortConverter $sortConverter,
36
        ContentVersionMatcher $versionMatcher
37
    )
38
    {
39 80
        $this->contentMatcher = $contentMatcher;
40 80
        $this->sectionMatcher = $sectionMatcher;
41 80
        $this->userMatcher = $userMatcher;
42 80
        $this->objectStateMatcher = $objectStateMatcher;
43 80
        $this->objectStateGroupMatcher = $objectStateGroupMatcher;
44 80
        $this->fieldHandlerManager = $fieldHandlerManager;
45 80
        $this->locationManager = $locationManager;
46 80
        $this->sortConverter = $sortConverter;
47 80
        $this->versionMatcher = $versionMatcher;
48 80
    }
49
50
    protected function load($step)
51
    {
52
        $versionCollection = $this->matchVersions('load', $step);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->matchVersions('load', $step); of type array|ArrayObject adds the type array to the return on line 56 which is incompatible with the return type of the parent method Kaliop\eZMigrationBundle...or\ContentManager::load of type Kaliop\eZMigrationBundle...\ContentCollection|null.
Loading history...
53
54
        $this->setReferences($versionCollection, $step);
0 ignored issues
show
Documentation introduced by
$versionCollection is of type array|object<ArrayObject>, but the function expects a object<Object>|object<Ka...ion\AbstractCollection>.

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...
55
56
        return $versionCollection;
57
    }
58
59
    /**
60
     * Handles the content delete migration action type
61
     */
62 View Code Duplication
    protected function delete($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...
63
    {
64
        $versionCollection = $this->matchVersions('delete', $step);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->matchVersions('delete', $step); of type array|ArrayObject adds the type array to the return on line 79 which is incompatible with the return type of the parent method Kaliop\eZMigrationBundle...\ContentManager::delete of type Kaliop\eZMigrationBundle...\ContentCollection|null.
Loading history...
65
66
        $this->setReferences($versionCollection, $step);
0 ignored issues
show
Documentation introduced by
$versionCollection is of type array|object<ArrayObject>, but the function expects a object<Object>|object<Ka...ion\AbstractCollection>.

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...
67
68
        $contentService = $this->repository->getContentService();
69
70
        foreach ($versionCollection as $versionInfo) {
71
            try {
72
                $contentService->deleteVersion($versionInfo);
73
            } catch (NotFoundException $e) {
74
                // Someone else (or even us, by virtue of location tree?) removed the content which we found just a
75
                // second ago. We can safely ignore this
76
            }
77
        }
78
79
        return $versionCollection;
80
    }
81
82
    /**
83
     * @param string $action
84
     * @return ContentCollection
85
     * @throws \Exception
86
     */
87
    protected function matchVersions($action, $step)
88
    {
89 View Code Duplication
        if (!isset($step->dsl['object_id']) && !isset($step->dsl['remote_id']) && !isset($step->dsl['match'])) {
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...
90
            throw new \Exception("The id or remote id of an object or a match condition is required to $action a content version");
91
        }
92
93
        if (!isset($step->dsl['match_versions']) && !isset($step->dsl['versions'])) {
94
            throw new \Exception("A verision match condition is required to $action a content version");
95
        }
96
97
        // Backwards compat
98
99 View Code Duplication
        if (isset($step->dsl['match'])) {
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...
100
            $match = $step->dsl['match'];
101
        } else {
102
            if (isset($step->dsl['object_id'])) {
103
                $match = array('content_id' => $step->dsl['object_id']);
104
            } elseif (isset($step->dsl['remote_id'])) {
105
                $match = array('content_remote_id' => $step->dsl['remote_id']);
106
            }
107
        }
108
109
        if (isset($step->dsl['match_versions'])) {
110
            $matchVersions = $step->dsl['match_versions'];
111
        } else {
112
            $matchVersions = array(ContentVersionMatcher::MATCH_VERSION => $step->dsl['versions']);
113
        }
114
115
        // convert the references passed in the match
116
        $match = $this->resolveReferencesRecursively($match);
0 ignored issues
show
Bug introduced by
The variable $match does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
117
        $matchVersions = $this->resolveReferencesRecursively($matchVersions);
118
119
        $sort = isset($step->dsl['match_sort']) ? $this->referenceResolver->resolveReference($step->dsl['match_sort']) : array();
120
        $offset = isset($step->dsl['match_offset']) ? $this->referenceResolver->resolveReference($step->dsl['match_offset']) : 0;
121
        $limit = isset($step->dsl['match_limit']) ? $this->referenceResolver->resolveReference($step->dsl['match_limit']) : 0;
122
123
        return $this->versionMatcher->match($match, $matchVersions, $sort, $offset, $limit);
124
    }
125
126
    /**
127
     * @param VersionInfo $versionInfo
128
     * @param array $references
129
     * @param $step
130
     * @return array
131
     *
132
     * @todo allow setting more refs: creation date, modification date, creator id, langauge codes
133
     */
134
    protected function getReferencesValues($versionInfo, array $references, $step)
135
    {
136
        $refs = array();
137
138
        foreach ($references as $reference) {
139
            switch ($reference['attribute']) {
140
                case 'version_no':
141
                    $value = $versionInfo->versionNo;
142
                    break;
143
                case 'version_status':
144
                    $value = $this->versionStatusToHash($versionInfo->status);
145
                    break;
146
                default:
147
                    // NB: this will generate an error if the user tries to seta  ref to a field value
148
                    $value = reset(parent::getReferencesValues($versionInfo, array($references), $step));
0 ignored issues
show
Bug introduced by
parent::getReferencesVal...ay($references), $step) cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
Documentation introduced by
$versionInfo is of type object<eZ\Publish\API\Re...es\Content\VersionInfo>, but the function expects a object<eZ\Publish\API\Re...Values\Content\Content>.

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...
149
            }
150
151
            $refs[$reference['identifier']] = $value;
152
        }
153
154
        return $refs;
155
    }
156
157
    protected function versionStatusToHash($status)
158
    {
159
        foreach(ContentVersionMatcher::STATUS_MAP as $own => $ez) {
160
            if ($status == $ez) {
161
                return $own;
162
            }
163
        }
164
165
        /// @todo log warning?
166
        return $status;
167
    }
168
}
169