AbstractStorage::find()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace codenixsv\Patterns\Behavioral\ChainOfResponsibility;
5
6
/**
7
 * Class AbstractStorage
8
 * @package codenixsv\Patterns\Behavioral\ChainOfResponsibility
9
 */
10
abstract class AbstractStorage implements Storage
11
{
12
    /**
13
     * @var AbstractStorage|null
14
     */
15
    protected $successor;
16
17
    /**
18
     * @var bool
19
     */
20
    protected $isAvailable = false;
21
22
    /**
23
     * @param bool $isAvailable
24
     */
25 3
    public function setIsAvailable(bool $isAvailable): void
26
    {
27 3
        $this->isAvailable = $isAvailable;
28 3
    }
29
30
    /**
31
     * @param AbstractStorage $storage
32
     */
33 3
    public function setNext(AbstractStorage $storage)
34
    {
35 3
        $this->successor = $storage;
36 3
    }
37
38
    /**
39
     * @param int $id
40
     * @return string
41
     */
42
    abstract protected function findProductById(int $id): string ;
43
44
    /**
45
     * @return bool
46
     */
47 3
    protected function isAvailable(): bool
48
    {
49 3
        return $this->isAvailable;
50
    }
51
52
    /**
53
     * @param int $id
54
     * @return string
55
     * @throws \Exception
56
     */
57 3
    public function find(int $id): string
58
    {
59 3
        if ($this->isAvailable()) {
60 2
            return $this->findProductById($id);
61
        }
62
63 2
        if ($this->successor) {
64 2
            return $this->successor->find($id);
65
        }
66
67 1
        throw new \Exception('Product not found');
68
    }
69
}
70