Completed
Push — master ( ccf2a7...46a6ef )
by Korotkov
04:33 queued 02:12
created

Interpreter::getValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
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)
41
    {
42
        if (count($this->depository->getItems())) {
43
44
            $values = explode(' ', $input);
45
46
            foreach ($values as $value) {
47
                if (is_numeric($value)) {
48
                    $this->item = $this->depository->getItem($value - 1);
49
                }
50
            }
51
52
            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...
53
                foreach ($values as $value) {
54
                    switch ($value) {
55
                        case 'author':
56
                            printf('%s: %s', ' Author', $this->item->getAuthor());
57
                            break;
58
                        case 'album':
59
                            printf('%s: %s', ' Album', $this->item->getAlbum());
60
                            break;
61
                    }
62
                }
63
            }
64
65
            printf('%s', "\n");
66
        }
67
    }
68
}
69