Completed
Push — master ( af9880...c194aa )
by Tristan
02:05
created

InMemoryRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Enzyme\Axiom\Repositories;
4
5
use Enzyme\Axiom\Bags\BagInterface;
6
use Enzyme\Axiom\Atoms\IntegerAtom;
7
use Enzyme\Axiom\Models\ModelInterface;
8
use Enzyme\Axiom\Factories\FactoryInterface;
9
10
class InMemoryRepository implements RepositoryInterface
11
{
12
    protected $factory;
13
    protected $store;
14
15
    public function __construct(FactoryInterface $factory)
16
    {
17
        $this->factory = $factory;
18
        $this->store = [];
19
    }
20
21
    public function add(ModelInterface $model)
22
    {
23
        $this->store[$model->identity()] = $model;
24
    }
25
26
    public function removeById(IntegerAtom $id)
27
    {
28
        if ($this->has($id)) {
29
            unset($this->store[$id->getValue()]);
30
        }
31
    }
32
33
    public function update(ModelInterface $model, BagInterface $data)
34
    {
35
        $updated_model = $this->factory->update($model, $data);
36
        $this->store[$model->identity()] = $updated_model;
37
    }
38
39
    public function getById(IntegerAtom $id)
40
    {
41
        return $this->has($id)
42
             ? $this->store[$id->getValue()]
43
             : null;
44
    }
45
46
    public function getAll()
47
    {
48
        return $this->store;
49
    }
50
51
    public function has(IntegerAtom $id)
52
    {
53
        return isset($this->store[$id->getValue()])
54
            && array_key_exists($id->getValue(), $this->store);
55
    }
56
}
57