Completed
Push — master ( 21def9...8c5037 )
by Gaetano
21:26
created

ObjectStateGroupMatcher::findAllStateGroups()   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
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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_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_NOT:
68
                    return new ObjectStateGroupCollection(array_diff_key($this->findAllObjectStateGroups(), $this->matchObjectStateGroup($values)->getArrayCopy()));
69
            }
70
        }
71
    }
72
73
    /**
74
     * @param int[] $objectStateGroupIds
75
     * @return ObjectStateGroup[]
76
     */
77
    protected function findObjectStateGroupsById(array $objectStateGroupIds)
78
    {
79
        $objectStateGroups = [];
80
81
        foreach ($objectStateGroupIds as $objectStateGroupId) {
82
            // return unique contents
83
            $objectStateGroup = $this->repository->getObjectStateService()->loadObjectStateGroup($objectStateGroupId);
84
            $objectStateGroups[$objectStateGroup->id] = $objectStateGroup;
85
        }
86
87
        return $objectStateGroups;
88
    }
89
90
        /**
91
         * @param int[] $objectStateGroupIdentifiers
92
         * @return ObjectStateGroup[]
93
         */
94
    protected function findObjectStateGroupsByIdentifier(array $objectStateGroupIdentifiers)
95
    {
96
        $objectStateGroups = [];
97
98
        $groupsByIdentifier = $this->loadAvailableStateGroups();
99
100
        foreach ($objectStateGroupIdentifiers as $objectStateGroupIdentifier) {
101
            if (!array_key_exists($objectStateGroupIdentifier, $groupsByIdentifier)) {
102
                throw new NotFoundException("ObjectStateGroup", $objectStateGroupIdentifier);
103
            }
104
            // return unique contents
105
            $objectStateGroups[$groupsByIdentifier[$objectStateGroupIdentifier]->id] = $groupsByIdentifier[$objectStateGroupIdentifier];
106
        }
107
108
        return $objectStateGroups;
109
    }
110
111
    /**
112
     * @return ObjectStateGroup[] key: the group identifier
113
     */
114
    protected function findAllObjectStateGroups()
115
    {
116
        return $this->loadAvailableStateGroups();
117
    }
118
119
    /**
120
     * @return ObjectStateGroup[] key: the group identifier
121
     */
122
    protected function loadAvailableStateGroups()
123
    {
124
        $stateGroupsList = [];
125
        $objectStateService = $this->repository->getObjectStateService();
126
127
        foreach ($objectStateService->loadObjectStateGroups() as $group) {
128
            $stateGroupsList[$group->identifier] = $group;
129
        }
130
131
        return $stateGroupsList;
132
    }
133
}
134