EntityCriteriaConfigurator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 75.76%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 76
ccs 25
cts 33
cp 0.7576
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B configure() 0 26 6
B filterDoctrineProperty() 0 19 5
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
use ScayTrase\Api\Cruds\ReferenceProviderInterface;
11
12
final class EntityCriteriaConfigurator implements CriteriaConfiguratorInterface
13
{
14
    const ALIAS_SEPARATOR = '.';
15
16
    /** @var PropertyMapperInterface */
17
    private $mapper;
18
    /** @var ReferenceProviderInterface */
19
    private $provider;
20
21
    /**
22
     * DoctrineCriteriaConfigurator constructor.
23
     *
24
     * @param PropertyMapperInterface    $mapper
25
     * @param ReferenceProviderInterface $provider
26
     */
27 14
    public function __construct(PropertyMapperInterface $mapper, ReferenceProviderInterface $provider)
28
    {
29 14
        $this->mapper   = $mapper;
30 14
        $this->provider = $provider;
31 14
    }
32
33
    /** {@inheritdoc} */
34 14
    public function configure(string $fqcn, Criteria $criteria, $arguments)
35
    {
36 14
        if (null === $arguments) {
37
            return;
38
        }
39
40 14
        if (!is_array($arguments)) {
41
            throw CriteriaConfigurationException::invalidType('array|null', gettype($criteria));
42
        }
43
44 14
        foreach ((array)$arguments as $apiProperty => $value) {
45
            try {
46 12
                $mappedProperty = $this->mapper->getEntityProperty($fqcn, $apiProperty);
47
48 12
                if (null === $mappedProperty) {
49
                    throw CriteriaConfigurationException::invalidData($apiProperty);
50
                }
51
52 12
                $value = $this->provider->getEntityReference($fqcn, $mappedProperty, $value);
53 12
                $this->filterDoctrineProperty($criteria, $mappedProperty, $value);
54 12
                unset($arguments[$apiProperty]);
55
            } catch (MapperException $e) {
56 12
                throw CriteriaConfigurationException::invalidProperty($apiProperty, $e);
57
            }
58
        }
59 14
    }
60
61
    /**
62
     * @param Criteria $criteria
63
     * @param string   $property
64
     * @param mixed    $value
65
     *
66
     * @throws CriteriaConfigurationException
67
     */
68 12
    private function filterDoctrineProperty(Criteria $criteria, $property, $value)
69
    {
70
        switch (true) {
71 12
            case is_array($value):
72 4
                $criteria->andWhere(Criteria::expr()->in($property, $value));
73 4
                break;
74 12
            case null === $value:
75 4
                $criteria->andWhere(Criteria::expr()->isNull($property));
76 4
                break;
77 12
            case !is_scalar($value) && !is_object($value):
78
                throw CriteriaConfigurationException::invalidPropertyType(
79
                    $property,
80
                    'scalar|array|null|identifier',
81
                    gettype($value)
82
                );
83
            default:
84 12
                $criteria->andWhere(Criteria::expr()->eq($property, $value));
85
        }
86 12
    }
87
}
88