Completed
Push — 1.5 ( 0839f7...e525f5 )
by Colin
02:31
created

HeadingPermalinkProcessor   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 12
dl 0
loc 98
ccs 35
cts 45
cp 0.7778
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfiguration() 0 4 1
A __invoke() 0 13 4
A useSlugGeneratorFromConfigurationIfProvided() 0 13 4
A __construct() 0 8 2
A addHeadingLink() 0 24 4
A getChildText() 0 13 5
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\Extension\HeadingPermalink\SlugGenerator\DefaultSlugGenerator;
19
use League\CommonMark\Extension\HeadingPermalink\SlugGenerator\SlugGeneratorInterface;
20
use League\CommonMark\Inline\Element\Code;
21
use League\CommonMark\Inline\Element\Text;
22
use League\CommonMark\Node\Node;
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 SlugGeneratorInterface|DeprecatedSlugGeneratorInterface */
35
    private $slugGenerator;
36
37
    /** @var ConfigurationInterface */
38
    private $config;
39
40
    /**
41
     * @param SlugGeneratorInterface|DeprecatedSlugGeneratorInterface|null $slugGenerator
42
     */
43 75
    public function __construct($slugGenerator = null)
44
    {
45 75
        if ($slugGenerator instanceof DeprecatedSlugGeneratorInterface) {
46
            @trigger_error(sprintf('Passing a %s into the %s constructor is deprecated; use a %s instead', DeprecatedSlugGeneratorInterface::class, self::class, SlugGeneratorInterface::class), E_USER_DEPRECATED);
47
        }
48
49 75
        $this->slugGenerator = $slugGenerator ?? new DefaultSlugGenerator();
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->useSlugGeneratorFromConfigurationIfProvided();
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 useSlugGeneratorFromConfigurationIfProvided(): void
72
    {
73 75
        $generator = $this->config->get('heading_permalink/slug_generator');
74 75
        if ($generator === null) {
75 69
            return;
76
        }
77
78 6
        if (!($generator instanceof DeprecatedSlugGeneratorInterface || $generator instanceof SlugGeneratorInterface)) {
79 3
            throw new InvalidOptionException('The heading_permalink/slug_generator option must be an instance of ' . SlugGeneratorInterface::class);
80
        }
81
82 3
        $this->slugGenerator = $generator;
83 3
    }
84
85 69
    private function addHeadingLink(Heading $heading): void
86
    {
87 69
        if ($this->slugGenerator instanceof DeprecatedSlugGeneratorInterface) {
88
            $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...
89
            $slug = $this->slugGenerator->createSlug($text);
90
        } else {
91 69
            $slug = $this->slugGenerator->generateSlug($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
    private function getChildText(Node $node): string
114
    {
115
        $text = '';
116
117
        $walker = $node->walker();
118
        while ($event = $walker->next()) {
119
            if ($event->isEntering() && (($child = $event->getNode()) instanceof Text || $child instanceof Code)) {
120
                $text .= $child->getContent();
121
            }
122
        }
123
124
        return $text;
125
    }
126
}
127