Completed
Push — master ( 4ff9cc...589f5d )
by Pavel
07:24
created

EntityCriteriaConfigurator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 64
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B configure() 0 23 6
A filterDoctrineProperty() 0 15 4
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
    public function __construct(PropertyMapperInterface $mapper)
23
    {
24
        $this->mapper = $mapper;
25
    }
26
27
    /** {@inheritdoc} */
28
    public function configure($fqcn, Criteria $criteria, $arguments)
29
    {
30
        if (null === $arguments) {
31
            return;
32
        }
33
34
        if (!is_array($arguments)) {
35
            throw CriteriaConfigurationException::invalidType('array|null', gettype($criteria));
36
        }
37
38
        try {
39
            foreach ((array)$arguments as $apiProperty => $value) {
40
                $mappedProperty = $this->mapper->getObjectProperty($fqcn, $apiProperty);
41
42
                if (null !== $mappedProperty) {
43
                    $this->filterDoctrineProperty($criteria, $mappedProperty, $value);
44
                    unset($arguments[$apiProperty]);
45
                }
46
            }
47
        } catch (MapperException $e) {
48
            throw new CriteriaConfigurationException(sprintf('Error getting object property: %s', $e->getMessage()));
49
        }
50
    }
51
52
    /**
53
     * @param Criteria $criteria
54
     * @param string   $property
55
     * @param mixed    $value
56
     *
57
     * @throws CriteriaConfigurationException
58
     */
59
    private function filterDoctrineProperty(Criteria $criteria, $property, $value)
60
    {
61
        switch (true) {
62
            case is_array($value):
63
                $criteria->andWhere(Criteria::expr()->in($property, $value));
64
                break;
65
            case null === $value:
66
                $criteria->andWhere(Criteria::expr()->isNull($property));
67
                break;
68
            case !is_scalar($value):
69
                throw CriteriaConfigurationException::invalid($property, $value);
70
            default:
71
                $criteria->andWhere(Criteria::expr()->eq($property, $value));
72
        }
73
    }
74
}
75