Completed
Push — master ( f283e7...8452cb )
by Korotkov
02:56 queued 10s
created

Interpreter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 implements InterpreterInterface
17
{
18
    /**
19
     * @var DepositoryInterface
20
     */
21
    protected $depository;
22
23
    /**
24
     * Interpreter constructor.
25
     * @param DepositoryInterface $depository
26
     */
27
    public function __construct(DepositoryInterface $depository)
28
    {
29
        $this->depository = $depository;
30
    }
31
32
    /**
33
     * @param string $input
34
     */
35
    public function interpret(string $input = null): void
36
    {
37
        if (!isset($input)) {
38
            throw new \InvalidArgumentException();
39
        }
40
41
        $item  = null;
42
        $input = explode(" ", $input);
43
44
        foreach ($input as $value) {
45
            if (is_numeric($value)) {
46
                $item = $this->getDepository()->getItem($value - 1);
47
            }
48
        }
49
50
        if (!isset($item)) {
51
            throw new \InvalidArgumentException();
52
        }
53
54
        foreach ($input as $value) {
55
            if ($value == "album") {
56
                printf("%s ", $item->getName());
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Behavioral\Interpreter\ItemInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Behavioral\Interpreter\ItemInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
                printf("%s ", $item->/** @scrutinizer ignore-call */ getName());
Loading history...
57
            }
58
59
            if ($value == "author") {
60
                printf("%s", $item->getAuthor());
0 ignored issues
show
Bug introduced by
The method getAuthor() does not exist on Behavioral\Interpreter\ItemInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Behavioral\Interpreter\ItemInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
                printf("%s", $item->/** @scrutinizer ignore-call */ getAuthor());
Loading history...
61
            }
62
        }
63
64
        printf("%s", PHP_EOL);
65
    }
66
67
    /**
68
     * @return DepositoryInterface
69
     */
70
    public function getDepository(): DepositoryInterface
71
    {
72
        return $this->depository;
73
    }
74
}
75