Completed
Push — master ( 5a501b...3982a6 )
by Gaetano
08:04
created

getLimitationArrayWithIdentifiers()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 38

Duplication

Lines 12
Ratio 31.58 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 12
loc 38
ccs 0
cts 36
cp 0
rs 7.6666
c 0
b 0
f 0
cc 10
nc 10
nop 1
crap 110

How to fix   Complexity   

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\Helper;
4
5
use eZ\Publish\API\Repository\Values\User\Limitation;
6
use Kaliop\eZMigrationBundle\Core\Matcher\LocationMatcher;
7
use Kaliop\eZMigrationBundle\Core\Matcher\SectionMatcher;
8
use Kaliop\eZMigrationBundle\Core\Matcher\ContentTypeMatcher;
9
10
class LimitationConverter
11
{
12
    protected $locationMatcher;
13
    protected $sectionMatcher;
14
    protected $contentTypeMatcher;
15
16
    /** @var array  */
17
    protected $siteAccessList = array();
18
19
    public function __construct(
20
        array $siteAccessList,
21
        LocationMatcher $locationMatcher,
22
        SectionMatcher $sectionMatcher,
23
        ContentTypeMatcher $contentTypeMatcher
24
    )
25
    {
26
        $this->locationMatcher = $locationMatcher;
27
        $this->sectionMatcher = $sectionMatcher;
28
        $this->contentTypeMatcher = $contentTypeMatcher;
29
30
        foreach ($siteAccessList as $siteAccess) {
31
            $id = sprintf('%u', crc32($siteAccess));
32
            $this->siteAccessList[$id] = $siteAccess;
33
        }
34
    }
35
36
    /**
37
     * Used f.e. to generate a human-readable role definition: substitute entity ids with their identifiers/names
38
     *
39
     * @param Limitation $limitation
40
     * @return array keys: identifier, values
41
     */
42
    public function getLimitationArrayWithIdentifiers(Limitation $limitation)
43
    {
44
        $retValues = array();
45
        $values = $limitation->limitationValues;
46
47
        switch ($limitation->getIdentifier()) {
48
            case 'Section':
49 View Code Duplication
            case 'NewSection':
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...
50
                foreach ($values as $value) {
51
                    $section = $this->sectionMatcher->matchOneByKey($value);
52
                    $retValues[] = $section->identifier;
53
                }
54
                break;
55
            case 'SiteAccess':
56
                foreach ($values as $value) {
57
                    $name = "No/unknown SiteAccess Found";
58
                    if (array_key_exists($value, $this->siteAccessList)) {
59
                        $name = $this->siteAccessList[$value];
60
                    }
61
                    $retValues[] = $name;
62
                }
63
                break;
64
            case 'ParentClass':
65 View Code Duplication
            case 'Class':
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...
66
                foreach ($values as $value) {
67
                    $contentType = $this->contentTypeMatcher->matchOneByKey($value);
68
                    $retValues[] = $contentType->identifier;
69
                }
70
                break;
71
            default:
72
                $retValues = $values;
73
        }
74
75
        return array(
76
            'identifier' => $limitation->getIdentifier(),
77
            'values' => $retValues
78
        );
79
    }
80
81
    /**
82
     * Converts human readable limitation values to their numeric counterparts we get the array in 2 parts so the
83
     * referencing can be completed on it before it's sent to this method.
84
     *
85
     * @param $limitationIdentifier
86
     * @param array $values
87
     * @return array
88
     * @throws \Exception
89
     */
90
    public function resolveLimitationValue($limitationIdentifier, array $values)
91
    {
92
        $retValues = array();
93
        switch ($limitationIdentifier) {
94
            // q: is it worth doing this for nodes? does it make a translation at all?
95
            case 'Node':
96
                foreach ($values as $value) {
97
                    $location = $this->locationMatcher->matchOneByKey($value);
98
                    $retValues[] = $location->id;
99
                }
100
                break;
101
            case 'Section':
102 View Code Duplication
            case 'NewSection':
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...
103
                foreach ($values as $value) {
104
                    $section = $this->sectionMatcher->matchOneByKey($value);
105
                    $retValues[] = $section->id;
106
                }
107
                break;
108
            case 'SiteAccess':
109
                $siteAccesses = array_flip($this->siteAccessList);
110
                foreach ($values as $value) {
111
                    if (!isset($siteAccesses[$value])) {
112
                        throw new \Exception("SiteAccess '$value' is not configured");
113
                    }
114
                    $retValues[] = (string)$siteAccesses[$value];
115
                }
116
                break;
117
            case 'ParentClass':
118 View Code Duplication
            case 'Class':
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...
119
                foreach ($values as $value) {
120
                    $contentType = $this->contentTypeMatcher->matchOneByKey($value);
121
                    $retValues[] = $contentType->id;
122
                }
123
                break;
124
            default:
125
                $retValues = $values;
126
        }
127
128
        return $retValues;
129
    }
130
}
131