Completed
Branch master (e35419)
by Gaetano
06:40
created

TrashMatcher::matchItem()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 5
nop 1
dl 0
loc 18
ccs 12
cts 12
cp 1
crap 4
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Matcher;
4
5
use eZ\Publish\API\Repository\Values\Content\Query;
6
use Kaliop\eZMigrationBundle\API\Collection\TrashedItemCollection;
7
use Kaliop\eZMigrationBundle\API\Exception\InvalidMatchConditionsException;
8
9
/// q: is it better to extend Content or Location Matcher ?
10
class TrashMatcher extends ContentMatcher
11
{
12
    const MATCH_ITEM_ID = 'item_id';
13
14
    protected $allowedConditions = array(
15
        self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT,
16
        self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID,
17
        self::MATCH_ATTRIBUTE, self::MATCH_CONTENT_TYPE_ID, self::MATCH_CONTENT_TYPE_IDENTIFIER, self::MATCH_GROUP,
18
        self::MATCH_CREATION_DATE, self::MATCH_MODIFICATION_DATE, self::MATCH_OBJECT_STATE, self::MATCH_OWNER,
19
        self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID, self::MATCH_SECTION, self::MATCH_SUBTREE,
20
        self::MATCH_VISIBILITY,
21
        // aliases
22
        'content_type', 'content_type_id', 'content_type_identifier',
23
    );
24
25
    protected $returns = 'Trashed-Item';
26
27
    /**
28
     * @param array $conditions key: condition, value: int / string / int[] / string[]
29
     * @param array $sort
30
     * @param int $offset
31
     * @param int $limit
32
     * @return TrashedItemCollection
33
     * @throws InvalidMatchConditionsException
34
     */
35 1
    public function match(array $conditions, array $sort = array(), $offset = 0, $limit = 0)
36
    {
37
        /// @todo throw id we get passed sorting or offset
38
39 1
        return $this->matchItem($conditions);
40
    }
41
42
    /**
43
     * @param array $conditions
44
     * @return TrashedItemCollection
45
     * @throws InvalidMatchConditionsException
46
     *
47
     * @todo test all supported matching conditions
48
     * @todo support matching by item_id
49
     * @todo test if sorting and offset,limit do work
50
     */
51 1
    public function matchItem(array $conditions)
52
    {
53 1
        $this->validateConditions($conditions);
54
55 1
        foreach ($conditions as $key => $values) {
56
57 1
            $query = new Query();
58 1
            $query->limit = self::INT_MAX_16BIT;
59 1
            if (isset($query->performCount)) $query->performCount = false;
60 1
            $query->filter = $this->getQueryCriterion($key, $values);
61 1
            $results = $this->repository->getTrashService()->findTrashItems($query);
62
63 1
            $items = [];
64 1
            foreach ($results->items as $result) {
65 1
                $items[$result->id] = $result;
66
            }
67
68 1
            return new TrashedItemCollection($items);
69
        }
70
    }
71
}
72