Completed
Push — master ( f78604...98e267 )
by Pavel
04:30
created

EntityCriteriaConfigurator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 66.67%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 70
ccs 24
cts 36
cp 0.6667
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B configure() 0 29 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 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
                    var_dump(get_class($this->mapper));
0 ignored issues
show
Security Debugging Code introduced by
var_dump(get_class($this->mapper)); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
44
                    var_dump($fqcn);
45
                    var_dump($apiProperty);
46
47
                    throw CriteriaConfigurationException::invalidCriteriaConfiguration($apiProperty);
48
                }
49
50 2
                $this->filterDoctrineProperty($criteria, $mappedProperty, $value);
51 2
                unset($arguments[$apiProperty]);
52 2
            }
53 2
        } catch (MapperException $e) {
54
            throw new CriteriaConfigurationException(sprintf('Error getting object property: %s', $e->getMessage()));
55
        }
56 2
    }
57
58
    /**
59
     * @param Criteria $criteria
60
     * @param string   $property
61
     * @param mixed    $value
62
     *
63
     * @throws CriteriaConfigurationException
64
     */
65 2
    private function filterDoctrineProperty(Criteria $criteria, $property, $value)
66 1
    {
67 2
        switch (true) {
68 2
            case is_array($value):
69
                $criteria->andWhere(Criteria::expr()->in($property, $value));
70
                break;
71 2
            case null === $value:
72
                $criteria->andWhere(Criteria::expr()->isNull($property));
73
                break;
74 2
            case !is_scalar($value):
75
                throw CriteriaConfigurationException::invalid($property, $value);
76 2
            default:
77 2
                $criteria->andWhere(Criteria::expr()->eq($property, $value));
78 2
        }
79 2
    }
80
}
81