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

InMemoryRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 47
wmc 10
lcom 1
cbo 3
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A add() 0 4 1
A removeById() 0 6 2
A update() 0 5 1
A getById() 0 6 2
A getAll() 0 4 1
A has() 0 5 2
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