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

EmojiCoreProcessor::setConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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