Interpreter::interpret()   A
last analyzed

Complexity

Conditions 5
Paths 10

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 10
b 0
f 0
nc 10
nop 1
dl 0
loc 22
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author  : Jagepard <[email protected]>
7
 * @license https://mit-license.org/ MIT
8
 */
9
10
namespace Behavioral\Interpreter;
11
12
class Interpreter implements InterpreterInterface
13
{
14
    private array $registryData;
15
16
    /**
17
     * Receives filing data
18
     * --------------------------
19
     * Принимает данные картотеки
20
     *
21
     * @param  RegistryInterface $registry
22
     */
23
    public function __construct(RegistryInterface $registry)
24
    {
25
        $this->registryData = $registry->getData();
26
    }
27
28
    /**
29
     * Interpret the request according to the incoming $input string
30
     * --------------------------------------------------------------
31
     * Инерпретирует запрос в соответствии со входящей строкой $input
32
     *
33
     * @param  string $input
34
     * @return string
35
     */
36
    public function interpret(string $input): string
37
    {
38
        $num    = 0;
39
        $output = [];
40
41
        if (preg_match('~[0-9]+~', $input, $match)) {
42
            $num = $match[0] - 1;
43
        }
44
45
        if (!array_key_exists($num, $this->registryData)) {
46
            throw new \InvalidArgumentException("Element $num does not exist in the registry");
47
        }
48
49
        if (strpos($input, 'author') !== false) {
50
            $output[] = $this->registryData[$num]->getAuthor();
51
        }
52
53
        if (strpos($input, 'album') !== false) {
54
            $output[] = $this->registryData[$num]->getAlbum();
55
        }
56
57
        return implode(' ', $output) . "\n";
58
    }
59
}
60