Completed
Push — master ( 670f0c...07e238 )
by Pavel
12:30
created

EntityCriteriaConfigurator::configure()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.6829

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 11
cts 15
cp 0.7332
rs 8.439
c 0
b 0
f 0
cc 6
eloc 14
nc 7
nop 3
crap 6.6829
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 4
    public function __construct(PropertyMapperInterface $mapper)
23
    {
24 4
        $this->mapper = $mapper;
25 4
    }
26
27
    /** {@inheritdoc} */
28 4
    public function configure($fqcn, Criteria $criteria, $arguments)
29
    {
30 4
        if (null === $arguments) {
31
            return;
32
        }
33
34 4
        if (!is_array($arguments)) {
35
            throw CriteriaConfigurationException::invalidType('array|null', gettype($criteria));
36
        }
37
38 4
        foreach ((array)$arguments as $apiProperty => $value) {
39
            try {
40 4
                $mappedProperty = $this->mapper->getEntityProperty($fqcn, $apiProperty);
41
42 4
                if (null === $mappedProperty) {
43
                    throw CriteriaConfigurationException::invalidData($apiProperty);
44
                }
45
46 4
                $this->filterDoctrineProperty($criteria, $mappedProperty, $value);
47 4
                unset($arguments[$apiProperty]);
48 4
            } catch (MapperException $e) {
49
                throw CriteriaConfigurationException::invalidProperty($apiProperty, $e);
50
            }
51 4
        }
52 4
    }
53
54
    /**
55
     * @param Criteria $criteria
56
     * @param string   $property
57
     * @param mixed    $value
58
     *
59
     * @throws CriteriaConfigurationException
60
     */
61 4
    private function filterDoctrineProperty(Criteria $criteria, $property, $value)
62
    {
63 4
        switch (true) {
64 4
            case is_array($value):
65
                $criteria->andWhere(Criteria::expr()->in($property, $value));
66 1
                break;
67 4
            case null === $value:
68
                $criteria->andWhere(Criteria::expr()->isNull($property));
69
                break;
70 4
            case !is_scalar($value):
71
                throw CriteriaConfigurationException::invalidPropertyType(
72
                    $property,
73
                    'scalar|array|null',
74
                    gettype($value)
75
                );
76 4
            default:
77 4
                $criteria->andWhere(Criteria::expr()->eq($property, $value));
78 4
        }
79 4
    }
80
}
81