Completed
Push — master ( ef0e27...1f22bd )
by Takashi
17s queued 12s
created

EmojiManager::getImageUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 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 10
    public function __construct(private Proxy $esa)
14
    {
15 10
        $this->emojis = $this->flattenEmojis($this->esa->getEmojis());
16 10
    }
17
18 8
    public function getImageUrl(string $code): string
19
    {
20 8
        foreach ($this->emojis as $key => $url) {
21 8
            if ($key === $code) {
22 7
                return $url;
23
            }
24
        }
25
26 1
        throw new UndefinedEmojiException();
27
    }
28
29 10
    private function flattenEmojis(array $emojis): array
30
    {
31 10
        $flattened = [];
32
33 10
        foreach ($emojis as $emoji) {
34 10
            $flattened[$emoji['code']] = $emoji['url'];
35
36 10
            foreach ($emoji['aliases'] as $alias) {
37 10
                $flattened[$alias] = $emoji['url'];
38
            }
39
        }
40
41 10
        return $flattened;
42
    }
43
}
44