GatewayMock::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\UseCases\Tests\Mocks;
11
12
class GatewayMock
13
{
14
    /**
15
     * @var array<int, array>
16
     */
17
    private $collection = [];
18
19
    /**
20
     * Add item in collection and return id assigned
21
     */
22
    public function create(array $item): int
23
    {
24
        $itemId = $this->count() + 1;
25
        $this->collection[$itemId] = $item;
26
27
        return $itemId;
28
    }
29
30
    /**
31
     * Get item in collection
32
     */
33
    public function read(int $itemId): ?array
34
    {
35
        if (!$this->exist($itemId)) {
36
            return null;
37
        }
38
39
        return $this->collection[$itemId];
40
    }
41
42
    /**
43
     * Edit item in collection
44
     */
45
    public function update(int $itemId, array $item): bool
46
    {
47
        if (!$this->exist($itemId)) {
48
            return false;
49
        }
50
51
        $this->collection[$itemId] = $item;
52
53
        return true;
54
    }
55
56
    /**
57
     * Remove item in collection
58
     */
59
    public function delete(int $itemId): bool
60
    {
61
        if (!$this->exist($itemId)) {
62
            return false;
63
        }
64
65
        unset($this->collection[$itemId]);
66
67
        return true;
68
    }
69
70
    private function exist(int $itemId): bool
71
    {
72
        return !empty($this->collection[$itemId]);
73
    }
74
75
    private function count(): int
76
    {
77
        return \count($this->collection);
78
    }
79
}
80