Completed
Push — master ( 7785c8...07cf23 )
by Gaetano
09:22
created

ObjectStateGroupMatcher::match()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Matcher;
4
5
use Kaliop\eZMigrationBundle\API\Collection\ObjectStateGroupCollection;
6
use \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup;
7
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface;
8
9
class ObjectStateGroupMatcher extends AbstractMatcher implements KeyMatcherInterface
10
{
11
    use FlexibleKeyMatcherTrait;
12
13
    const MATCH_OBJECTSTATEGROUP_ID = 'objectstategroup_id';
14
    const MATCH_OBJECTSTATEGROUP_IDENTIFIER = 'objectstategroup_identifier';
15
16
    protected $allowedConditions = array(
17
        self::MATCH_OBJECTSTATEGROUP_ID, self::MATCH_OBJECTSTATEGROUP_IDENTIFIER,
18
        // aliases
19
        'id', 'identifier'
20
    );
21
    protected $returns = 'ObjectStateGroup';
22
23
    /**
24
     * @param array $conditions key: condition, value: int / string / int[] / string[]
25
     * @return ObjectStateGroupCollection
26
     */
27
    public function match(array $conditions)
28
    {
29
        return $this->matchObjectStateGroup($conditions);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->matchObjectStateGroup($conditions); of type Kaliop\eZMigrationBundle...ateGroupCollection|null adds the type Kaliop\eZMigrationBundle...ectStateGroupCollection to the return on line 29 which is incompatible with the return type declared by the interface Kaliop\eZMigrationBundle...MatcherInterface::match of type array|Kaliop\eZMigrationBundle\API\ArrayObject.
Loading history...
30
    }
31
32
    protected function getConditionsFromKey($key)
33
    {
34
        if (is_int($key) || ctype_digit($key)) {
35
            return array(self::MATCH_OBJECTSTATEGROUP_ID => $key);
36
        }
37
        return array(self::MATCH_OBJECTSTATEGROUP_IDENTIFIER => $key);
38
    }
39
40
    /**
41
     * @param array $conditions key: condition, value: int / string / int[] / string[]
42
     * @return ObjectStateGroupCollection
43
     */
44
    public function matchObjectStateGroup($conditions)
45
    {
46
        $this->validateConditions($conditions);
47
48
        foreach ($conditions as $key => $values) {
49
50
            if (!is_array($values)) {
51
                $values = array($values);
52
            }
53
54
            switch ($key) {
55
                case 'id':
56
                case self::MATCH_OBJECTSTATEGROUP_ID:
57
                    return new ObjectStateGroupCollection($this->findObjectStateGroupsById($values));
58
59
                case 'identifier':
60
                case self::MATCH_OBJECTSTATEGROUP_IDENTIFIER:
61
                    return new ObjectStateGroupCollection($this->findObjectStateGroupsByIdentifier($values));
62
            }
63
        }
64
    }
65
66
    /**
67
     * @param int[] $objectStateGroupIds
68
     * @return ObjectStateGroup[]
69
     */
70
    protected function findObjectStateGroupsById(array $objectStateGroupIds)
71
    {
72
        $objectStateGroups = [];
73
74
        foreach ($objectStateGroupIds as $objectStateGroupId) {
75
            // return unique contents
76
            $objectStateGroup = $this->repository->getObjectStateService()->loadObjectStateGroup($objectStateGroupId);
77
            $objectStateGroups[$objectStateGroup->id] = $objectStateGroup;
78
        }
79
80
        return $objectStateGroups;
81
    }
82
83
        /**
84
         * @param int[] $objectStateGroupIdentifiers
85
         * @return ObjectStateGroup[]
86
         */
87
    protected function findObjectStateGroupsByIdentifier(array $objectStateGroupIdentifiers)
88
    {
89
        $objectStateGroups = [];
90
91
        $groupsByIdentifier = $this->loadAvailableStateGroups();
92
93
        foreach ($objectStateGroupIdentifiers as $objectStateGroupIdentifier) {
94
            if (!array_key_exists($objectStateGroupIdentifier, $groupsByIdentifier)) {
95
                throw new NotFoundException("ObjectStateGroup", $objectStateGroupIdentifier);
96
            }
97
            // return unique contents
98
            $objectStateGroups[$groupsByIdentifier[$objectStateGroupIdentifier]->id] = $groupsByIdentifier[$objectStateGroupIdentifier];
99
        }
100
101
        return $objectStateGroups;
102
    }
103
104
    /**
105
     * @return ObjectStateGroup[] key: the group identifier
106
     */
107
    protected function loadAvailableStateGroups()
108
    {
109
        $stateGroupsList = [];
110
        $objectStateService = $this->repository->getObjectStateService();
111
112
        foreach ($objectStateService->loadObjectStateGroups() as $group) {
113
            $stateGroupsList[$group->identifier] = $group;
114
        }
115
116
        return $stateGroupsList;
117
    }
118
}
119