Completed
Push — master ( 07e238...0f15f1 )
by Pavel
09:14
created

EntityCriteriaConfigurator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Criteria;
4
5
use Doctrine\Common\Collections\Criteria;
6
use ScayTrase\Api\Cruds\CriteriaConfiguratorInterface;
7
use ScayTrase\Api\Cruds\Exception\CriteriaConfigurationException;
8
use ScayTrase\Api\Cruds\Exception\MapperException;
9
use ScayTrase\Api\Cruds\PropertyMapperInterface;
10
11
final class EntityCriteriaConfigurator implements CriteriaConfiguratorInterface
12
{
13
    const ALIAS_SEPARATOR = '.';
14
    /** @var  PropertyMapperInterface */
15
    private $mapper;
16
17
    /**
18
     * DoctrineCriteriaConfigurator constructor.
19
     *
20
     * @param PropertyMapperInterface $mapper
21
     */
22 8
    public function __construct(PropertyMapperInterface $mapper)
23
    {
24 8
        $this->mapper = $mapper;
25 8
    }
26
27
    /** {@inheritdoc} */
28 8
    public function configure($fqcn, Criteria $criteria, $arguments)
29
    {
30 8
        if (null === $arguments) {
31
            return;
32
        }
33
34 8
        if (!is_array($arguments)) {
35
            throw CriteriaConfigurationException::invalidType('array|null', gettype($criteria));
36
        }
37
38 8
        foreach ((array)$arguments as $apiProperty => $value) {
39
            try {
40 6
                $mappedProperty = $this->mapper->getEntityProperty($fqcn, $apiProperty);
41
42 6
                if (null === $mappedProperty) {
43
                    throw CriteriaConfigurationException::invalidData($apiProperty);
44
                }
45
46 6
                $this->filterDoctrineProperty($criteria, $mappedProperty, $value);
47 6
                unset($arguments[$apiProperty]);
48 6
            } catch (MapperException $e) {
49
                throw CriteriaConfigurationException::invalidProperty($apiProperty, $e);
50
            }
51 8
        }
52 8
    }
53
54
    /**
55
     * @param Criteria $criteria
56
     * @param string   $property
57
     * @param mixed    $value
58
     *
59
     * @throws CriteriaConfigurationException
60
     */
61 6
    private function filterDoctrineProperty(Criteria $criteria, $property, $value)
62
    {
63 6
        switch (true) {
64 6
            case is_array($value):
65 2
                $criteria->andWhere(Criteria::expr()->in($property, $value));
66 3
                break;
67 6
            case null === $value:
68 2
                $criteria->andWhere(Criteria::expr()->isNull($property));
69 2
                break;
70 6
            case !is_scalar($value):
71
                throw CriteriaConfigurationException::invalidPropertyType(
72
                    $property,
73
                    'scalar|array|null',
74
                    gettype($value)
75
                );
76 6
            default:
77 6
                $criteria->andWhere(Criteria::expr()->eq($property, $value));
78 6
        }
79 6
    }
80
}
81