Completed
Pull Request — master (#24)
by Quentin
03:30
created

InMemoryLoaderTrait::setUp()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
ccs 0
cts 17
cp 0
rs 9.2
cc 4
eloc 11
nc 3
nop 2
crap 20
1
<?php
2
3
namespace Majora\Framework\Loader\Bridge\InMemory;
4
5
use Majora\Framework\Loader\LazyLoaderTrait;
6
use Majora\Framework\Model\CollectionableInterface;
7
use Majora\Framework\Model\EntityCollection;
8
use Majora\Framework\Normalizer\MajoraNormalizer;
9
10
/**
11
 * Base trait for fixtures repository.
12
 */
13
trait InMemoryLoaderTrait
14
{
15
    use LazyLoaderTrait;
16
17
    /**
18
     * @var EntityCollection
19
     */
20
    protected $entityCollection;
21
22
    /**
23
     * @var MajoraNormalizer
24
     */
25
    protected $normalizer;
26
27
    /**
28
     * Construct.
29
     *
30
     * @param string           $collectionClass
31
     * @param MajoraNormalizer $normalizer
32
     */
33
    public function __construct($collectionClass, MajoraNormalizer $normalizer)
34
    {
35
        $this->setUp($collectionClass, $normalizer);
36
    }
37
38
    /**
39
     * Substitue to __construct() in order to easier call from parent class.
40
     *
41
     * @param string           $collectionClass
42
     * @param MajoraNormalizer $normalizer
43
     */
44
    private function setUp($collectionClass, MajoraNormalizer $normalizer)
45
    {
46
        if (empty($collectionClass) || !class_exists($collectionClass)) {
47
            throw new \InvalidArgumentException(sprintf(
48
                'You must provide a valid EntityCollection class name, "%s" given.',
49
                $collectionClass
50
            ));
51
        }
52
        $this->entityCollection = new $collectionClass();
53
        if (!$this->entityCollection instanceof EntityCollection) {
54
            throw new \InvalidArgumentException(sprintf(
55
                'Provided class name is not an Majora\Framework\Model\EntityCollection, "%s" given.',
56
                $collectionClass
57
            ));
58
        }
59
60
        $this->normalizer = $normalizer;
61
    }
62
63
    /**
64
     * Register given set of data into datastore.
65
     *
66
     * @param array $entityData
67
     */
68
    public function registerData(array $entityData)
69
    {
70
        foreach ($entityData as $data) {
71
            $this->registerEntity($this->normalizer->denormalize(
72
                $data,
73
                $this->entityCollection->getEntityClass()
74
            ));
75
        }
76
    }
77
78
    /**
79
     * Register a new Collectionable entity into datastore.
80
     *
81
     * @param CollectionableInterface $entity
82
     *
83
     * @throws \InvalidArgumentException If given object is not a supported type
84
     */
85
    public function registerEntity(CollectionableInterface $entity)
86
    {
87
        if (!is_a($entity, $this->entityCollection->getEntityClass())) {
88
            throw new \InvalidArgumentException(sprintf('Only "%s" object allowed into "%s" store, "%s" given.',
89
                $this->entityCollection->getEntityClass(),
90
                get_class($this),
91
                get_class($entity)
92
            ));
93
        }
94
95
        $this->entityCollection->set(
96
            $entity->getId(),
97
            $this->loadDelegates($entity)
98
        );
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     *
104
     * @see LoaderInterface::retrieveAll()
105
     */
106
    public function retrieveAll(array $filters = array(), $limit = null, $offset = null)
107
    {
108
        $result = clone $this->entityCollection;
109
        if (!empty($filters)) {
110
            $result = $result->search($filters);
111
        }
112
        if ($offset) {
113
            $result = $result->cslice($offset, $limit);
114
        }
115
        if ($limit && !$offset) {
116
            $result = $result->chunk($limit);
117
        }
118
119
        return $result;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     *
125
     * @see LoaderInterface::retrieveOne()
126
     */
127
    public function retrieveOne(array $filters = array())
0 ignored issues
show
Unused Code introduced by
The parameter $filters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
    {
129
        return $this->retrieveAll()->first();
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     *
135
     * @see LoaderInterface::retrieve()
136
     */
137
    public function retrieve($id)
138
    {
139
        return $this->entityCollection->get($id);
140
    }
141
}
142