Test Setup Failed
Pull Request — latest (#3)
by Mark
34:22
created

EmojiCoreProcessor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
eloc 17
c 1
b 0
f 1
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 27 5
A setConfiguration() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UnicornFail\Emoji\Extension;
6
7
use League\Configuration\ConfigurationAwareInterface;
8
use League\Configuration\ConfigurationInterface;
9
use UnicornFail\Emoji\Event\DocumentParsedEvent;
10
use UnicornFail\Emoji\Node\Inline\AbstractEmoji;
11
use UnicornFail\Emoji\Parser\Lexer;
12
13
/**
14
 * Processes all parsed Emoji nodes and set the various configurations.
15
 */
16
final class EmojiCoreProcessor implements ConfigurationAwareInterface
17
{
18
    /**
19
     * @var ConfigurationInterface
20
     *
21
     * @psalm-readonly-allow-private-mutation
22
     */
23
    private $config;
24
25
    public function setConfiguration(ConfigurationInterface $configuration): void
26
    {
27
        $this->config = $configuration;
28
    }
29
30
    public function __invoke(DocumentParsedEvent $e): void
31
    {
32
        $convertTo = (string) ($this->config->get('convertTo') ?? Lexer::UNICODE);
33
34
        /** @var string[] $excludedShortcodes */
35
        $excludedShortcodes = $this->config->get('exclude.shortcodes');
36
37
        /** @var ?int $presentation */
38
        $presentation = $this->config->get('presentation');
39
40
        // Ensure tokens are set to the correct stringable type.
41
        $walker = $e->getDocument()->walker();
42
        while ($event = $walker->next()) {
43
            if (! $event->isEntering()) {
44
                continue;
45
            }
46
47
            $emoji = $event->getNode();
48
            if (! ($emoji instanceof AbstractEmoji)) {
49
                continue;
50
            }
51
52
            $emoji->setConvertTo($convertTo);
53
            $emoji->setExcludedShortcodes($excludedShortcodes);
54
55
            if ($presentation !== null) {
56
                $emoji->setPresentationMode($presentation);
57
            }
58
        }
59
    }
60
}
61