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

loadAvailableStateGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 6
nc 2
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,
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());
0 ignored issues
show
Bug introduced by
The method findAllObjectStateGroups() does not seem to exist on object<Kaliop\eZMigratio...bjectStateGroupMatcher>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
            }
67
        }
68
    }
69
70
    /**
71
     * @param int[] $objectStateGroupIds
72
     * @return ObjectStateGroup[]
73
     */
74
    protected function findObjectStateGroupsById(array $objectStateGroupIds)
75
    {
76
        $objectStateGroups = [];
77
78
        foreach ($objectStateGroupIds as $objectStateGroupId) {
79
            // return unique contents
80
            $objectStateGroup = $this->repository->getObjectStateService()->loadObjectStateGroup($objectStateGroupId);
81
            $objectStateGroups[$objectStateGroup->id] = $objectStateGroup;
82
        }
83
84
        return $objectStateGroups;
85
    }
86
87
        /**
88
         * @param int[] $objectStateGroupIdentifiers
89
         * @return ObjectStateGroup[]
90
         */
91
    protected function findObjectStateGroupsByIdentifier(array $objectStateGroupIdentifiers)
92
    {
93
        $objectStateGroups = [];
94
95
        $groupsByIdentifier = $this->loadAvailableStateGroups();
96
97
        foreach ($objectStateGroupIdentifiers as $objectStateGroupIdentifier) {
98
            if (!array_key_exists($objectStateGroupIdentifier, $groupsByIdentifier)) {
99
                throw new NotFoundException("ObjectStateGroup", $objectStateGroupIdentifier);
100
            }
101
            // return unique contents
102
            $objectStateGroups[$groupsByIdentifier[$objectStateGroupIdentifier]->id] = $groupsByIdentifier[$objectStateGroupIdentifier];
103
        }
104
105
        return $objectStateGroups;
106
    }
107
108
    /**
109
     * @return ObjectStateGroup[] key: the group identifier
110
     */
111
    protected function findAllStateGroups()
112
    {
113
        return $this->loadAvailableStateGroups();
114
    }
115
116
    /**
117
     * @return ObjectStateGroup[] key: the group identifier
118
     */
119
    protected function loadAvailableStateGroups()
120
    {
121
        $stateGroupsList = [];
122
        $objectStateService = $this->repository->getObjectStateService();
123
124
        foreach ($objectStateService->loadObjectStateGroups() as $group) {
125
            $stateGroupsList[$group->identifier] = $group;
126
        }
127
128
        return $stateGroupsList;
129
    }
130
}
131