Passed
Push — master ( 11dcca...383fee )
by Korotkov
02:21
created

Interpreter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A interpret() 0 9 2
A setItem() 0 5 3
B printValue() 0 11 5
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author    : Korotkov Danila <[email protected]>
7
 * @license   https://mit-license.org/ MIT
8
 */
9
10
namespace Behavioral\Interpreter;
11
12
/**
13
 * Class Interpreter
14
 * @package Behavioral\Interpreter
15
 */
16
class Interpreter
17
{
18
19
    /**
20
     * @var Item
21
     */
22
    protected $item;
23
    /**
24
     * @var Depository
25
     */
26
    protected $depository;
27
28
    /**
29
     * Interpreter constructor.
30
     * @param Depository $depository
31
     */
32
    public function __construct(Depository $depository)
33
    {
34
        $this->depository = $depository;
35
    }
36
37
    /**
38
     * @param string $input
39
     */
40
    public function interpret(string $input): void
41
    {
42
        if (count($this->depository->getItems())) {
43
            $values = explode(' ', $input);
44
45
            $this->setItem($values);
46
            $this->printValue($values);
47
48
            printf('%s', "\n");
49
        }
50
    }
51
52
    /**
53
     * @param array $values
54
     */
55
    protected function setItem(array $values): void
56
    {
57
        foreach ($values as $value) {
58
            if (is_numeric($value)) {
59
                $this->item = $this->depository->getItem($value - 1);
60
            }
61
        }
62
    }
63
64
    /**
65
     * @param array $values
66
     */
67
    protected function printValue(array $values): void
68
    {
69
        if ($this->item instanceof Item) {
0 ignored issues
show
introduced by
$this->item is always a sub-type of Behavioral\Interpreter\Item. If $this->item can have other possible types, add them to src/Interpreter.php:20.
Loading history...
70
            foreach ($values as $value) {
71
                switch ($value) {
72
                    case 'author':
73
                        printf('%s: %s', ' Author', $this->item->getAuthor());
74
                        break;
75
                    case 'album':
76
                        printf('%s: %s', ' Album', $this->item->getAlbum());
77
                        break;
78
                }
79
            }
80
        }
81
    }
82
}
83