Completed
Push — master ( 98e267...f8cf59 )
by Pavel
04:25
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 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
ccs 11
cts 15
cp 0.7332
rs 8.439
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 2
    public function configure($fqcn, Criteria $criteria, $arguments)
29
    {
30 2
        if (null === $arguments) {
31
            return;
32
        }
33
34 2
        if (!is_array($arguments)) {
35
            throw CriteriaConfigurationException::invalidType('array|null', gettype($criteria));
36
        }
37
38
        try {
39 2
            foreach ((array)$arguments as $apiProperty => $value) {
40 2
                $mappedProperty = $this->mapper->getObjectProperty($fqcn, $apiProperty);
41
42 2
                if (null === $mappedProperty) {
43
                    throw CriteriaConfigurationException::invalidCriteriaConfiguration($apiProperty);
44
                }
45
46 2
                $this->filterDoctrineProperty($criteria, $mappedProperty, $value);
47 2
                unset($arguments[$apiProperty]);
48 2
            }
49 2
        } catch (MapperException $e) {
50
            throw new CriteriaConfigurationException(sprintf('Error getting object property: %s', $e->getMessage()));
51
        }
52 2
    }
53
54
    /**
55
     * @param Criteria $criteria
56
     * @param string   $property
57
     * @param mixed    $value
58
     *
59
     * @throws CriteriaConfigurationException
60
     */
61 2
    private function filterDoctrineProperty(Criteria $criteria, $property, $value)
62
    {
63 2
        switch (true) {
64 2
            case is_array($value):
65
                $criteria->andWhere(Criteria::expr()->in($property, $value));
66 1
                break;
67 2
            case null === $value:
68
                $criteria->andWhere(Criteria::expr()->isNull($property));
69
                break;
70 2
            case !is_scalar($value):
71
                throw CriteriaConfigurationException::invalid($property, $value);
72 2
            default:
73 2
                $criteria->andWhere(Criteria::expr()->eq($property, $value));
74 2
        }
75 2
    }
76
}
77