Total Complexity | 9 |
Total Lines | 76 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
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() |
||
85 | } |
||
86 | } |
||
87 |