Passed
Pull Request — master (#15)
by Takashi
07:09 queued 51s
created

EmojiManager::__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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Esa;
6
7
use App\Esa\Exception\UndefinedEmojiException;
8
9
class EmojiManager
10
{
11
    private $emojis;
12
13
    public function __construct(private Proxy $esa)
14
    {
15
        $this->emojis = $this->flattenEmojis($this->esa->getEmojis());
16
    }
17
18
    public function getImageUrl(string $code): string
19
    {
20
        foreach ($this->emojis as $key => $url) {
21
            if ($key === $code) {
22
                return $url;
23
            }
24
        }
25
26
        throw new UndefinedEmojiException();
27
    }
28
29
    private function flattenEmojis(array $emojis): array
30
    {
31
        $flattened = [];
32
33
        foreach ($emojis as $emoji) {
34
            $flattened[$emoji['code']] = $emoji['url'];
35
36
            foreach ($emoji['aliases'] as $alias) {
37
                $flattened[$alias] = $emoji['url'];
38
            }
39
        }
40
41
        return $flattened;
42
    }
43
}
44