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

TwemojiProcessor::__invoke()   C

Complexity

Conditions 14
Paths 32

Size

Total Lines 77
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 43
c 1
b 0
f 1
dl 0
loc 77
rs 6.2666
cc 14
nc 32
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace UnicornFail\Emoji\Extension\Twemoji;
6
7
use League\Configuration\ConfigurationAwareInterface;
8
use League\Configuration\ConfigurationInterface;
9
use UnicornFail\Emoji\EmojiConverterInterface;
10
use UnicornFail\Emoji\Event\DocumentParsedEvent;
11
use UnicornFail\Emoji\Node\Emoji;
12
use UnicornFail\Emoji\Node\Image;
13
use UnicornFail\Emoji\Util\HtmlElement;
14
15
/**
16
 * Replaces emojis with Twemoji images.
17
 */
18
final class TwemojiProcessor implements ConfigurationAwareInterface
19
{
20
    public const CONVERSION_TYPE = 'twemoji';
21
22
    /**
23
     * @var ConfigurationInterface
24
     *
25
     * @psalm-readonly-allow-private-mutation
26
     * @psalm-suppress PropertyNotSetInConstructor
27
     */
28
    private $config;
29
30
    public function __invoke(DocumentParsedEvent $e): void
31
    {
32
        $urlBase = (string) $this->config->get('twemoji.urlBase');
33
34
        $classPrefix = (string) $this->config->get('twemoji.classPrefix');
35
36
        /** @var ?int $size */
37
        $size = $this->config->get('twemoji.size');
38
39
        $inlineSvg = (bool) $this->config->get('twemoji.inlineSvg');
40
41
        $type = (string) $this->config->get('twemoji.type');
42
43
        $walker = $e->getDocument()->walker();
44
        while ($event = $walker->next()) {
45
            $node = $event->getNode();
46
            if (! ($node instanceof Emoji) || $node->hexcode === null) {
47
                continue;
48
            }
49
50
            $parsedType     = $node->getParsedType();
51
            $configPath     = 'convert.' . (EmojiConverterInterface::TYPES[$parsedType] ?? '');
52
            $conversionType = null;
53
54
            if ($this->config->exists($configPath)) {
55
                $conversionType = (string) ($this->config->get($configPath) ?? '');
56
            }
57
58
            // Only convert types that are set to "twemoji".
59
            if ($conversionType !== self::CONVERSION_TYPE) {
60
                continue;
61
            }
62
63
            $url = \sprintf(
64
                '%s/%s/%s.%s',
65
                $urlBase,
66
                $type === 'png' ? '72x72' : 'svg',
67
                \strtolower($node->hexcode),
68
                $type
69
            );
70
71
            $image = new Image($node->getParsedValue(), $node, $url, $node->annotation);
72
73
            /** @var string[] $classes */
74
            $classes = (array) $this->config->get('twemoji.classes');
75
            $image->addClass(...$classes);
76
77
            if ($node->annotation !== null) {
78
                $image->addClass(HtmlElement::cleanCssIdentifier($classPrefix
79
                    ? $classPrefix . $node->annotation
80
                    : $node->annotation));
81
            }
82
83
            switch ($type) {
84
                case 'png':
85
                    // Set default size for PNG images.
86
                    if ($size === null) {
87
                        $size = 72;
88
                    }
89
90
                    if ($size) {
91
                        $image->attributes->set('height', (string) $size);
92
                        $image->attributes->set('width', (string) $size);
93
                    }
94
95
                    break;
96
97
                case 'svg':
98
                    // Ensure SVG images aren't massive and relative to its surroundings.
99
                    if ($inlineSvg) {
100
                        $image->attributes->set('style', 'width: 1em; height: 1em;');
101
                    }
102
103
                    break;
104
            }
105
106
            $node->replaceWith($image);
107
        }
108
    }
109
110
    public function setConfiguration(ConfigurationInterface $configuration): void
111
    {
112
        $this->config = $configuration;
113
    }
114
}
115