Test Failed
Push — docs ( 541d16...38ea2c )
by Mark
35:24
created

TwemojiProcessor::__invoke()   C

Complexity

Conditions 15
Paths 24

Size

Total Lines 66
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 38
nc 24
nop 1
dl 0
loc 66
rs 5.9166
c 0
b 0
f 0

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 League\Emoji\Extension\Twemoji;
6
7
use League\Configuration\ConfigurationAwareInterface;
8
use League\Configuration\ConfigurationInterface;
9
use League\Emoji\EmojiConverterInterface;
10
use League\Emoji\Event\DocumentParsedEvent;
11
use League\Emoji\Node\Emoji;
12
use League\Emoji\Node\Image;
13
use League\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|float|string|null $size */
37
        $size = $this->config->get('twemoji.size');
38
39
        $inline = (bool) $this->config->get('twemoji.inline');
40
41
        $type = (string) $this->config->get('twemoji.type');
42
43
        foreach ($e->getDocument()->getNodes() as $node) {
44
            if (! ($node instanceof Emoji) || $node->hexcode === null) {
45
                continue;
46
            }
47
48
            $parsedType     = $node->getParsedType();
49
            $configPath     = 'convert.' . (EmojiConverterInterface::TYPES[$parsedType] ?? '');
50
            $conversionType = null;
51
52
            if ($this->config->exists($configPath)) {
53
                $conversionType = (string) ($this->config->get($configPath) ?? '');
54
            }
55
56
            // Only convert types that are set to "twemoji".
57
            if ($conversionType !== self::CONVERSION_TYPE) {
58
                continue;
59
            }
60
61
            $url = \sprintf(
62
                '%s/%s/%s.%s',
63
                $urlBase,
64
                $type === 'png' ? '72x72' : 'svg',
65
                \strtolower($node->hexcode),
66
                $type
67
            );
68
69
            $image = new Image($node->getParsedValue(), $node, $url, $node->annotation, $node->annotation);
70
71
            /** @var string[] $classes */
72
            $classes = (array) $this->config->get('twemoji.classes');
73
            $image->addClass(...$classes);
74
75
            if ($node->annotation !== null) {
76
                $image->addClass(HtmlElement::cleanCssIdentifier($classPrefix
77
                    ? $classPrefix . $node->annotation
78
                    : $node->annotation));
79
            }
80
81
            // Ensure image isn't massive and relative to its surroundings by inlining it.
82
            if ($inline && $size === null) {
83
                $image->setAttribute('style', 'width: 1em; height: 1em; vertical-align: middle;');
84
            } elseif ($inline && $size !== null) {
85
                if (! \is_string($size)) {
86
                    $size .= 'em';
87
                }
88
89
                $image->setAttribute('style', \sprintf('width: %s; height: %s; vertical-align: middle;', $size, $size));
90
            } elseif ($size !== null) {
91
                $image->setAttribute('height', (string) $size);
92
                $image->setAttribute('width', (string) $size);
93
            }
94
95
            $node->replaceWith($image);
96
        }
97
    }
98
99
    public function setConfiguration(ConfigurationInterface $configuration): void
100
    {
101
        $this->config = $configuration;
102
    }
103
}
104