Passed
Push — master ( 4753c3...bca814 )
by Gaetano
09:41
created

findAllObjectStateGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Matcher;
4
5
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
6
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup;
7
use eZ\Publish\Core\Base\Exceptions\NotFoundException as CoreNotFoundException;
8
use Kaliop\eZMigrationBundle\API\Collection\ObjectStateGroupCollection;
9
use Kaliop\eZMigrationBundle\API\Exception\InvalidMatchConditionsException;
10
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface;
11
12
class ObjectStateGroupMatcher extends RepositoryMatcher implements KeyMatcherInterface
13
{
14
    use FlexibleKeyMatcherTrait;
15
16
    const MATCH_OBJECTSTATEGROUP_ID = 'object_state_group_id';
17
    const MATCH_OBJECTSTATEGROUP_IDENTIFIER = 'object_state_group_identifier';
18
19
    protected $allowedConditions = array(
20
        self:: MATCH_ALL, self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT,
21
        self::MATCH_OBJECTSTATEGROUP_ID, self::MATCH_OBJECTSTATEGROUP_IDENTIFIER,
22
        // aliases
23
        'id', 'identifier',
24
        // BC
25
        'objectstategroup_id', 'objectstategroup_identifier',
26
    );
27
    protected $returns = 'ObjectStateGroup';
28
29
    /**
30
     * @param array $conditions key: condition, value: int / string / int[] / string[]
31 4
     * @param bool $tolerateMisses
32
     * @return ObjectStateGroupCollection
33 4
     * @throws InvalidMatchConditionsException
34
     * @throws NotFoundException
35
     */
36 1
    public function match(array $conditions, $tolerateMisses = false)
37
    {
38 1
        return $this->matchObjectStateGroup($conditions);
39 1
    }
40
41 1
    protected function getConditionsFromKey($key)
42
    {
43
        if (is_int($key) || ctype_digit($key)) {
44
            return array(self::MATCH_OBJECTSTATEGROUP_ID => $key);
45
        }
46
        return array(self::MATCH_OBJECTSTATEGROUP_IDENTIFIER => $key);
47
    }
48
49 4
    /**
50
     * @param array $conditions key: condition, value: int / string / int[] / string[]
51 4
     * @param bool $tolerateMisses
52
     * @return ObjectStateGroupCollection
53 4
     * @throws InvalidMatchConditionsException
54
     * @throws NotFoundException
55 4
     */
56 4
    public function matchObjectStateGroup($conditions, $tolerateMisses = false)
57
    {
58
        $this->validateConditions($conditions);
59
60 4
        foreach ($conditions as $key => $values) {
61 4
62 1
            if (!is_array($values)) {
63
                $values = array($values);
64 4
            }
65 4
66 4
            switch ($key) {
67
                case 'id':
68
                case 'objectstategroup_id':
69
                case self::MATCH_OBJECTSTATEGROUP_ID:
70
                    return new ObjectStateGroupCollection($this->findObjectStateGroupsById($values, $tolerateMisses));
71
72
                case 'identifier':
73
                case 'objectstategroup_identifier':
74
                case self::MATCH_OBJECTSTATEGROUP_IDENTIFIER:
75
                    return new ObjectStateGroupCollection($this->findObjectStateGroupsByIdentifier($values, $tolerateMisses));
76
77
                case self::MATCH_ALL:
78
                    return new ObjectStateGroupCollection($this->findAllObjectStateGroups());
79
80
                case self::MATCH_AND:
81
                    return $this->matchAnd($values, $tolerateMisses);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->matchAnd($values, $tolerateMisses) returns the type array which is incompatible with the documented return type Kaliop\eZMigrationBundle...ectStateGroupCollection.
Loading history...
82
83
                case self::MATCH_OR:
84
                    return $this->matchOr($values, $tolerateMisses);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->matchOr($values, $tolerateMisses) returns the type array which is incompatible with the documented return type Kaliop\eZMigrationBundle...ectStateGroupCollection.
Loading history...
85
86
                case self::MATCH_NOT:
87 1
                    return new ObjectStateGroupCollection(array_diff_key($this->findAllObjectStateGroups(), $this->matchObjectStateGroup($values, true)->getArrayCopy()));
88
            }
89 1
        }
90
    }
91 1
92
    /**
93 1
     * @param int[] $objectStateGroupIds
94 1
     * @param bool $tolerateMisses
95
     * @return ObjectStateGroup[]
96
     * @throws NotFoundException
97 1
     */
98
    protected function findObjectStateGroupsById(array $objectStateGroupIds, $tolerateMisses = false)
99
    {
100
        $objectStateGroups = [];
101
102
        foreach ($objectStateGroupIds as $objectStateGroupId) {
103
            try {
104 4
                // return unique contents
105
                $objectStateGroup = $this->repository->getObjectStateService()->loadObjectStateGroup($objectStateGroupId);
106 4
                $objectStateGroups[$objectStateGroup->id] = $objectStateGroup;
107
            } catch(NotFoundException $e) {
108 4
                if (!$tolerateMisses) {
109
                    throw $e;
110 4
                }
111 4
            }
112
        }
113
114
        return $objectStateGroups;
115 4
    }
116
117
        /**
118 4
         * @param int[] $objectStateGroupIdentifiers
119
         * @param bool $tolerateMisses
120
         * @return ObjectStateGroup[]
121
         * @throws NotFoundException
122
         */
123
    protected function findObjectStateGroupsByIdentifier(array $objectStateGroupIdentifiers, $tolerateMisses = false)
124
    {
125
        $objectStateGroups = [];
126
127
        $groupsByIdentifier = $this->loadAvailableStateGroups();
128
129
        foreach ($objectStateGroupIdentifiers as $objectStateGroupIdentifier) {
130
            if (!array_key_exists($objectStateGroupIdentifier, $groupsByIdentifier)) {
131
                if ($tolerateMisses) {
132 4
                    continue;
133
                }
134 4
                throw new CoreNotFoundException("ObjectStateGroup", $objectStateGroupIdentifier);
135 4
            }
136
            // return unique contents
137 4
            $objectStateGroups[$groupsByIdentifier[$objectStateGroupIdentifier]->id] = $groupsByIdentifier[$objectStateGroupIdentifier];
138 4
        }
139
140
        return $objectStateGroups;
141 4
    }
142
143
    /**
144
     * @return ObjectStateGroup[] key: the group identifier
145
     */
146
    protected function findAllObjectStateGroups()
147
    {
148
        return $this->loadAvailableStateGroups();
149
    }
150
151
    /**
152
     * @return ObjectStateGroup[] key: the group identifier
153
     */
154
    protected function loadAvailableStateGroups()
155
    {
156
        $stateGroupsList = [];
157
        $objectStateService = $this->repository->getObjectStateService();
158
159
        foreach ($objectStateService->loadObjectStateGroups() as $group) {
160
            $stateGroupsList[$group->identifier] = $group;
161
        }
162
163
        return $stateGroupsList;
164
    }
165
}
166