Completed
Push — master ( 7dd66b...51fb44 )
by Phil
05:05
created

AbstractStore   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 7
c 7
b 2
f 0
lcom 1
cbo 4
dl 0
loc 53
ccs 0
cts 23
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B validate() 0 24 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->toArray([], 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
                throw new ValidationException($e->getMessage());
59
            }
60
        }
61
62
        return true;
63
    }
64
}
65