Passed
Push — master ( de30cb...30b342 )
by Korotkov
08:04 queued 13s
created

Interpreter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 12
Bugs 0 Features 2
Metric Value
eloc 15
c 12
b 0
f 2
dl 0
loc 46
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A interpret() 0 11 3
A getDataFromRegistry() 0 15 4
A __construct() 0 3 1
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
    public function __construct(RegistryInterface $registry)
17
    {
18
        $this->registryData = $registry->getData();
19
    }
20
21
    /**
22
     * @param string $input
23
     * @return array
24
     */
25
    public function interpret(string $input): array
26
    {
27
        $input = explode(' ', $input);
28
29
        foreach ($input as $value) {
30
            if (is_numeric($value)) {
31
                return $this->getDataFromRegistry($input, $this->registryData[$value - 1]);
32
            }
33
        }
34
35
        throw new \InvalidArgumentException("No id specified");
36
    }
37
38
    /**
39
     * @param array $input
40
     * @param AlbumInterface $item
41
     * @return array
42
     */
43
    private function getDataFromRegistry(array $input, AlbumInterface $item): array
44
    {
45
        $dataArray = [];
46
47
        foreach ($input as $value) {
48
            if ($value === "author") {
49
                $dataArray["author"] = $item->getAuthor();
50
            }
51
52
            if ($value === "album") {
53
                $dataArray["album"] = $item->getAlbum();
54
            }
55
        }
56
57
        return $dataArray;
58
    }
59
}
60