Completed
Branch develop (049341)
by Freddie
02:29
created

GatewayMock::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace FlexPHP\Repositories\Tests\Mocks;
4
5
/**
6
 * Class GatewayMock
7
 * @package FlexPHP\Repositories\Tests\Mocks
8
 */
9
class GatewayMock
10
{
11
    private $collection = [];
12
13
    /**
14
     * Add item in collection and return id assigned
15
     *
16
     * @param array $item
17
     * @return int
18
     */
19
    public function create(array $item): int
20
    {
21
        $id = $this->count() + 1;
22
        $this->collection[$id] = $item;
23
24
        return $id;
25
    }
26
27
    /**
28
     * Get item in collection
29
     *
30
     * @param int $id
31
     * @return array|null
32
     */
33
    public function read(int $id): ?array
34
    {
35
        if (!$this->exist($id)) {
36
            return null;
37
        }
38
39
        return $this->collection[$id];
40
    }
41
42
    /**
43
     * Edit item in collection
44
     *
45
     * @param int $id
46
     * @param array $item
47
     * @return bool
48
     */
49
    public function update(int $id, array $item): bool
50
    {
51
        if (!$this->exist($id)) {
52
            return false;
53
        }
54
55
        $this->collection[$id] = $item;
56
57
        return true;
58
    }
59
60
    /**
61
     * Remove item in collection
62
     *
63
     * @param int $id
64
     * @return bool
65
     */
66
    public function delete(int $id): bool
67
    {
68
        if (!$this->exist($id)) {
69
            return false;
70
        }
71
72
        unset($this->collection[$id]);
73
74
        return true;
75
    }
76
77
    private function exist(int $id)
78
    {
79
        return !empty($this->collection[$id]);
80
    }
81
82
    private function count()
83
    {
84
        return \count($this->collection);
85
    }
86
}
87