Completed
Push — master ( 1bf270...21def9 )
by Gaetano
06:55
created

SectionMatcher   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 110
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 6
dl 110
loc 110
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A match() 4 4 1
C matchSection() 24 24 8
A getConditionsFromKey() 7 7 3
A findSectionsById() 12 12 2
A findSectionsByIdentifier() 12 12 2
A findAllSections() 11 11 2

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\Matcher;
4
5
use eZ\Publish\API\Repository\Values\Content\Section;
6
use Kaliop\eZMigrationBundle\API\Collection\SectionCollection;
7
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface;
8
9 View Code Duplication
class SectionMatcher extends RepositoryMatcher implements KeyMatcherInterface
0 ignored issues
show
Duplication introduced by
This class 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...
10
{
11
    use FlexibleKeyMatcherTrait;
12
13
    const MATCH_SECTION_ID = 'section_id';
14
    const MATCH_SECTION_IDENTIFIER = 'section_identifier';
15
16
    protected $allowedConditions = array(
17
        self::MATCH_ALL,
18
        self::MATCH_SECTION_ID, self::MATCH_SECTION_IDENTIFIER,
19
        // aliases
20
        'id', 'identifier'
21
    );
22
    protected $returns = 'Role';
23
24
    /**
25
     * @param array $conditions key: condition, value: int / string / int[] / string[]
26
     * @return SectionCollection
27
     */
28
    public function match(array $conditions)
29
    {
30
        return $this->matchSection($conditions);
31
    }
32
33
    /**
34
     * @param array $conditions key: condition, value: int / string / int[] / string[]
35
     * @return SectionCollection
36
     */
37
    public function matchSection(array $conditions)
38
    {
39
        $this->validateConditions($conditions);
40
41
        foreach ($conditions as $key => $values) {
42
43
            if (!is_array($values)) {
44
                $values = array($values);
45
            }
46
47
            switch ($key) {
48
                case 'id':
49
                case self::MATCH_SECTION_ID:
50
                   return new SectionCollection($this->findSectionsById($values));
51
52
                case 'identifier':
53
                case self::MATCH_SECTION_IDENTIFIER:
54
                    return new SectionCollection($this->findSectionsByIdentifier($values));
55
56
                case self::MATCH_ALL:
57
                    return new SectionCollection($this->findAllSections());
58
            }
59
        }
60
    }
61
62
    protected function getConditionsFromKey($key)
63
    {
64
        if (is_int($key) || ctype_digit($key)) {
65
            return array(self::MATCH_SECTION_ID => $key);
66
        }
67
        return array(self::MATCH_SECTION_IDENTIFIER => $key);
68
    }
69
70
    /**
71
     * @param int[] $sectionIds
72
     * @return Section[]
73
     */
74
    protected function findSectionsById(array $sectionIds)
75
    {
76
        $sections = [];
77
78
        foreach ($sectionIds as $sectionId) {
79
            // return unique contents
80
            $section = $this->repository->getSectionService()->loadSection($sectionId);
81
            $sections[$section->id] = $section;
82
        }
83
84
        return $sections;
85
    }
86
87
    /**
88
     * @param string[] $sectionIdentifiers
89
     * @return Section[]
90
     */
91
    protected function findSectionsByIdentifier(array $sectionIdentifiers)
92
    {
93
        $sections = [];
94
95
        foreach ($sectionIdentifiers as $sectionIdentifier) {
96
            // return unique contents
97
            $section = $this->repository->getSectionService()->loadSectionByIdentifier($sectionIdentifier);
98
            $sections[$section->id] = $section;
99
        }
100
101
        return $sections;
102
    }
103
104
    /**
105
     * @return Section[]
106
     */
107
    protected function findAllSections()
108
    {
109
        $sections = [];
110
111
        foreach ($this->repository->getSectionService()->loadSections() as $section) {
112
            // return unique contents
113
            $sections[$section->id] = $section;
114
        }
115
116
        return $sections;
117
    }
118
}
119