Test Setup Failed
Pull Request — latest (#3)
by Mark
33:50
created

EmojiConverter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 14
c 1
b 0
f 1
dl 0
loc 51
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A convert() 0 5 1
A __invoke() 0 3 1
A create() 0 6 1
A getEnvironment() 0 3 1
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UnicornFail\Emoji;
6
7
use UnicornFail\Emoji\Environment\Environment;
8
use UnicornFail\Emoji\Environment\EnvironmentInterface;
9
use UnicornFail\Emoji\Extension\EmojiCoreExtension;
10
use UnicornFail\Emoji\Output\RenderedContentInterface;
11
use UnicornFail\Emoji\Parser\EmojiParser;
12
use UnicornFail\Emoji\Parser\EmojiParserInterface;
13
use UnicornFail\Emoji\Renderer\DocumentRenderer;
14
use UnicornFail\Emoji\Renderer\DocumentRendererInterface;
15
16
class EmojiConverter implements EmojiConverterInterface
17
{
18
    /** @var EnvironmentInterface */
19
    private $environment;
20
21
    /** @var EmojiParserInterface */
22
    private $parser;
23
24
    /** @var DocumentRendererInterface */
25
    private $renderer;
26
27
    public function __construct(EnvironmentInterface $environment, ?EmojiParserInterface $parser = null, ?DocumentRendererInterface $renderer = null)
28
    {
29
        $this->environment = $environment;
30
        $this->parser      = $parser ?? new EmojiParser($environment);
31
        $this->renderer    = $renderer ?? new DocumentRenderer($environment);
32
    }
33
34
    /**
35
     * @param array<string, mixed> $config
36
     */
37
    public static function create(array $config = []): EmojiConverterInterface
38
    {
39
        $environment = new Environment($config);
40
        $environment->addExtension(new EmojiCoreExtension());
41
42
        return new self($environment);
43
    }
44
45
    /**
46
     * Converts all HTML entities, shortcodes or emoticons to emojis (unicode).
47
     *
48
     * @see EmojiConverterInterface::convert
49
     *
50
     * @throws \RuntimeException
51
     */
52
    public function __invoke(string $input): RenderedContentInterface
53
    {
54
        return $this->convert($input);
55
    }
56
57
    public function convert(string $input): RenderedContentInterface
58
    {
59
        $document = $this->parser->parse($input);
60
61
        return $this->renderer->renderDocument($document);
62
    }
63
64
    public function getEnvironment(): EnvironmentInterface
65
    {
66
        return $this->environment;
67
    }
68
}
69