AbstractStore   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 56
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B validate() 0 27 6
1
<?php
2
3
namespace Percy\Store;
4
5
use Aura\Filter\Exception\FilterFailed;
6
use Aura\Filter\FilterFactory;
7
use InvalidArgumentException;
8
use Percy\Decorator\DecoratorTrait;
9
use Percy\Entity\Collection;
10
use Percy\Exception\ValidationException;
11
12
abstract class AbstractStore implements StoreInterface
13
{
14
    use DecoratorTrait;
15
16
    /**
17
     * @var \Aura\Filter\FilterFactory
18
     */
19
    protected $filter;
20
21
    /**
22
     * Construct.
23
     *
24
     * @param \Aura\Filter\FilterFactory
25
     */
26
    public function __construct(FilterFactory $filter)
27
    {
28
        $this->filter = $filter;
29
    }
30
31
    /**
32
     * Iterate collection and validate data.
33
     *
34
     * @param \Percy\Entity\Collection $collection
35
     *
36
     * @throws \Percy\Exception\ValidationException when first validation failure occurs
37
     *
38
     * @return boolean
39
     */
40
    public function validate(Collection $collection)
41
    {
42
        foreach ($collection->getIterator() as $entity) {
43
            if (is_null($entity->getValidator())) {
44
                continue;
45
            }
46
47
            $filter = $this->filter->newSubjectFilter($entity->getValidator());
48
49
            try {
50
                $data = $entity->getData([], false);
51
52
                foreach (array_keys($entity->getMapping()) as $property) {
53
                    $data[$property] = (array_key_exists($property, $data)) ? $data[$property] : null;
54
                }
55
56
                $filter($data);
57
            } catch (FilterFailed $e) {
58
                $exception = new ValidationException($e->getMessage());
59
                $exception->setFailures($e->getFailures()->getMessages());
60
61
                throw $exception;
62
            }
63
        }
64
65
        return true;
66
    }
67
}
68