Completed
Push — 1.5 ( 09e907...fb52dd )
by Colin
01:21
created

useNormalizerFromConfigurationIfProvided()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.8333
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 4
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\CommonMark\Extension\HeadingPermalink;
13
14
use League\CommonMark\Block\Element\Heading;
15
use League\CommonMark\Event\DocumentParsedEvent;
16
use League\CommonMark\Exception\InvalidOptionException;
17
use League\CommonMark\Extension\HeadingPermalink\Slug\SlugGeneratorInterface as DeprecatedSlugGeneratorInterface;
18
use League\CommonMark\Inline\Element\Code;
19
use League\CommonMark\Inline\Element\Text;
20
use League\CommonMark\Node\Node;
21
use League\CommonMark\Normalizer\SlugNormalizer;
22
use League\CommonMark\Normalizer\TextNormalizerInterface;
23
use League\CommonMark\Util\ConfigurationAwareInterface;
24
use League\CommonMark\Util\ConfigurationInterface;
25
26
/**
27
 * Searches the Document for Heading elements and adds HeadingPermalinks to each one
28
 */
29
final class HeadingPermalinkProcessor implements ConfigurationAwareInterface
30
{
31
    const INSERT_BEFORE = 'before';
32
    const INSERT_AFTER = 'after';
33
34
    /** @var TextNormalizerInterface|DeprecatedSlugGeneratorInterface */
35
    private $slugNormalizer;
36
37
    /** @var ConfigurationInterface */
38
    private $config;
39
40
    /**
41
     * @param TextNormalizerInterface|DeprecatedSlugGeneratorInterface|null $slugNormalizer
42
     */
43 75
    public function __construct($slugNormalizer = null)
44
    {
45 75
        if ($slugNormalizer instanceof DeprecatedSlugGeneratorInterface) {
46
            @trigger_error(sprintf('Passing a %s into the %s constructor is deprecated; use a %s instead', DeprecatedSlugGeneratorInterface::class, self::class, TextNormalizerInterface::class), E_USER_DEPRECATED);
47
        }
48
49 75
        $this->slugNormalizer = $slugNormalizer ?? new SlugNormalizer();
50 75
    }
51
52 75
    public function setConfiguration(ConfigurationInterface $configuration)
53
    {
54 75
        $this->config = $configuration;
55 75
    }
56
57 75
    public function __invoke(DocumentParsedEvent $e): void
58
    {
59 75
        $this->useNormalizerFromConfigurationIfProvided();
60
61 72
        $walker = $e->getDocument()->walker();
62
63 72
        while ($event = $walker->next()) {
64 72
            $node = $event->getNode();
65 72
            if ($node instanceof Heading && $event->isEntering()) {
66 69
                $this->addHeadingLink($node);
67
            }
68
        }
69 69
    }
70
71 75
    private function useNormalizerFromConfigurationIfProvided(): void
72
    {
73 75
        $generator = $this->config->get('heading_permalink/slug_normalizer');
74 75
        if ($generator === null) {
75 69
            return;
76
        }
77
78 6
        if (!($generator instanceof DeprecatedSlugGeneratorInterface || $generator instanceof TextNormalizerInterface)) {
79 3
            throw new InvalidOptionException('The heading_permalink/slug_normalizer option must be an instance of ' . TextNormalizerInterface::class);
80
        }
81
82 3
        $this->slugNormalizer = $generator;
83 3
    }
84
85 69
    private function addHeadingLink(Heading $heading): void
86
    {
87 69
        $text = $this->getChildText($heading);
0 ignored issues
show
Deprecated Code introduced by
The method League\CommonMark\Extens...ocessor::getChildText() has been deprecated with message: Not needed in 2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
88 69
        if ($this->slugNormalizer instanceof DeprecatedSlugGeneratorInterface) {
89
            $slug = $this->slugNormalizer->createSlug($text);
90
        } else {
91 69
            $slug = $this->slugNormalizer->normalize($text, $heading);
92
        }
93
94 69
        $headingLinkAnchor = new HeadingPermalink($slug);
95
96 69
        switch ($this->config->get('heading_permalink/insert', 'before')) {
97 69
            case self::INSERT_BEFORE:
98 57
                $heading->prependChild($headingLinkAnchor);
99
100 57
                return;
101 12
            case self::INSERT_AFTER:
102 9
                $heading->appendChild($headingLinkAnchor);
103
104 9
                return;
105
            default:
106 3
                throw new \RuntimeException("Invalid configuration value for heading_permalink/insert; expected 'before' or 'after'");
107
        }
108
    }
109
110
    /**
111
     * @deprecated Not needed in 2.0
112
     */
113 69
    private function getChildText(Node $node): string
114
    {
115 69
        $text = '';
116
117 69
        $walker = $node->walker();
118 69
        while ($event = $walker->next()) {
119 69
            if ($event->isEntering() && (($child = $event->getNode()) instanceof Text || $child instanceof Code)) {
120 69
                $text .= $child->getContent();
121
            }
122
        }
123
124 69
        return $text;
125
    }
126
}
127