Completed
Push — master ( 8c5037...eed80c )
by Gaetano
07:22
created

findObjectStateGroupsByIdentifier()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 8
nc 3
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 RepositoryMatcher 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_ALL, self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT,
18
        self::MATCH_OBJECTSTATEGROUP_ID, self::MATCH_OBJECTSTATEGROUP_IDENTIFIER,
19
        // aliases
20
        'id', 'identifier'
21
    );
22
    protected $returns = 'ObjectStateGroup';
23
24
    /**
25
     * @param array $conditions key: condition, value: int / string / int[] / string[]
26
     * @return ObjectStateGroupCollection
27
     */
28
    public function match(array $conditions)
29
    {
30
        return $this->matchObjectStateGroup($conditions);
31
    }
32
33
    protected function getConditionsFromKey($key)
34
    {
35
        if (is_int($key) || ctype_digit($key)) {
36
            return array(self::MATCH_OBJECTSTATEGROUP_ID => $key);
37
        }
38
        return array(self::MATCH_OBJECTSTATEGROUP_IDENTIFIER => $key);
39
    }
40
41
    /**
42
     * @param array $conditions key: condition, value: int / string / int[] / string[]
43
     * @return ObjectStateGroupCollection
44
     */
45
    public function matchObjectStateGroup($conditions)
46
    {
47
        $this->validateConditions($conditions);
48
49
        foreach ($conditions as $key => $values) {
50
51
            if (!is_array($values)) {
52
                $values = array($values);
53
            }
54
55
            switch ($key) {
56
                case 'id':
57
                case self::MATCH_OBJECTSTATEGROUP_ID:
58
                    return new ObjectStateGroupCollection($this->findObjectStateGroupsById($values));
59
60
                case 'identifier':
61
                case self::MATCH_OBJECTSTATEGROUP_IDENTIFIER:
62
                    return new ObjectStateGroupCollection($this->findObjectStateGroupsByIdentifier($values));
63
64
                case self::MATCH_ALL:
65
                    return new ObjectStateGroupCollection($this->findAllObjectStateGroups());
66
67
                case self::MATCH_AND:
68
                    return $this->matchAnd($values);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->matchAnd($values); of type object|array adds the type array to the return on line 68 which is incompatible with the return type documented by Kaliop\eZMigrationBundle...::matchObjectStateGroup of type Kaliop\eZMigrationBundle...ateGroupCollection|null.
Loading history...
69
70
                case self::MATCH_OR:
71
                    return $this->matchOr($values);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->matchOr($values); of type object|array adds the type array to the return on line 71 which is incompatible with the return type documented by Kaliop\eZMigrationBundle...::matchObjectStateGroup of type Kaliop\eZMigrationBundle...ateGroupCollection|null.
Loading history...
72
73
                case self::MATCH_NOT:
74
                    return new ObjectStateGroupCollection(array_diff_key($this->findAllObjectStateGroups(), $this->matchObjectStateGroup($values)->getArrayCopy()));
75
            }
76
        }
77
    }
78
79
    /**
80
     * @param int[] $objectStateGroupIds
81
     * @return ObjectStateGroup[]
82
     */
83
    protected function findObjectStateGroupsById(array $objectStateGroupIds)
84
    {
85
        $objectStateGroups = [];
86
87
        foreach ($objectStateGroupIds as $objectStateGroupId) {
88
            // return unique contents
89
            $objectStateGroup = $this->repository->getObjectStateService()->loadObjectStateGroup($objectStateGroupId);
90
            $objectStateGroups[$objectStateGroup->id] = $objectStateGroup;
91
        }
92
93
        return $objectStateGroups;
94
    }
95
96
        /**
97
         * @param int[] $objectStateGroupIdentifiers
98
         * @return ObjectStateGroup[]
99
         */
100
    protected function findObjectStateGroupsByIdentifier(array $objectStateGroupIdentifiers)
101
    {
102
        $objectStateGroups = [];
103
104
        $groupsByIdentifier = $this->loadAvailableStateGroups();
105
106
        foreach ($objectStateGroupIdentifiers as $objectStateGroupIdentifier) {
107
            if (!array_key_exists($objectStateGroupIdentifier, $groupsByIdentifier)) {
108
                throw new NotFoundException("ObjectStateGroup", $objectStateGroupIdentifier);
109
            }
110
            // return unique contents
111
            $objectStateGroups[$groupsByIdentifier[$objectStateGroupIdentifier]->id] = $groupsByIdentifier[$objectStateGroupIdentifier];
112
        }
113
114
        return $objectStateGroups;
115
    }
116
117
    /**
118
     * @return ObjectStateGroup[] key: the group identifier
119
     */
120
    protected function findAllObjectStateGroups()
121
    {
122
        return $this->loadAvailableStateGroups();
123
    }
124
125
    /**
126
     * @return ObjectStateGroup[] key: the group identifier
127
     */
128
    protected function loadAvailableStateGroups()
129
    {
130
        $stateGroupsList = [];
131
        $objectStateService = $this->repository->getObjectStateService();
132
133
        foreach ($objectStateService->loadObjectStateGroups() as $group) {
134
            $stateGroupsList[$group->identifier] = $group;
135
        }
136
137
        return $stateGroupsList;
138
    }
139
}
140