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

EmojiManager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 33
ccs 15
cts 15
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getImageUrl() 0 9 3
A flattenEmojis() 0 13 3
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