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

EmojiCoreProcessor   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 16
eloc 43
c 1
b 0
f 1
dl 0
loc 82
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C __invoke() 0 58 15
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\Emojibase\EmojibaseDatasetInterface;
10
use UnicornFail\Emoji\Event\DocumentParsedEvent;
11
use UnicornFail\Emoji\Node\Emoji;
12
use UnicornFail\Emoji\Parser\Lexer;
13
14
/**
15
 * Processes all parsed Emoji nodes and set the various configurations.
16
 */
17
final class EmojiCoreProcessor implements ConfigurationAwareInterface
18
{
19
    public const CONFIG_PATHS = [
20
        Lexer::T_EMOTICON    => 'convert.emoticons',
21
        Lexer::T_HTML_ENTITY => 'convert.html_entities',
22
        Lexer::T_SHORTCODE   => 'convert.shortcodes',
23
        Lexer::T_UNICODE     => 'convert.unicodes',
24
    ];
25
26
    /**
27
     * @var ConfigurationInterface
28
     *
29
     * @psalm-readonly-allow-private-mutation
30
     * @psalm-suppress PropertyNotSetInConstructor
31
     */
32
    private $config;
33
34
    public function __invoke(DocumentParsedEvent $e): void
35
    {
36
        /** @var string[] $excludedShortcodes */
37
        $excludedShortcodes = $this->config->get('exclude/shortcodes');
38
39
        /** @var ?int $presentation */
40
        $presentation = $this->config->get('presentation');
41
42
        // Ensure emojis are set to the correct stringable type.
43
        $walker = $e->getDocument()->walker();
44
        while ($event = $walker->next()) {
45
            if (! $event->isEntering()) {
46
                continue;
47
            }
48
49
            $node = $event->getNode();
50
            if (! ($node instanceof Emoji)) {
51
                continue;
52
            }
53
54
            $literal    = null;
55
            $type       = $node->getParsedType();
56
            $configPath = self::CONFIG_PATHS[$type] ?? null;
57
            $convertTo  = null;
58
59
            if ($configPath && $this->config->exists($configPath)) {
60
                $convertTo = (string) ($this->config->get($configPath) ?? '');
61
            }
62
63
            switch ($convertTo) {
64
                case Lexer::EMOTICON:
65
                    $literal = $node->emoticon;
66
                    break;
67
68
                case Lexer::HTML_ENTITY:
69
                    $literal = $node->htmlEntity;
70
                    break;
71
72
                case Lexer::SHORTCODE:
73
                    if ($shortcode = $node->getShortcode($excludedShortcodes, true)) {
74
                        $literal = $shortcode;
75
                    }
76
77
                    break;
78
79
                case Lexer::TEXT:
80
                case Lexer::UNICODE:
81
                    if (($presentation ?? $node->type) === EmojibaseDatasetInterface::TEXT && $node->text) {
82
                        $literal = $node->text;
83
                    } else {
84
                        $literal = $node->emoji ?? $node->unicode;
85
                    }
86
87
                    break;
88
            }
89
90
            if ($literal !== null) {
91
                $node->setLiteral($literal);
92
            }
93
        }
94
    }
95
96
    public function setConfiguration(ConfigurationInterface $configuration): void
97
    {
98
        $this->config = $configuration;
99
    }
100
}
101