ShortcodeManager   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 25
eloc 47
c 5
b 1
f 0
dl 0
loc 168
ccs 53
cts 53
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A register() 0 12 4
A registerAll() 0 7 2
A doShortcode() 0 22 4
A alias() 0 16 4
A deregister() 0 12 6
A preProcessTags() 0 11 3
A hasShortcode() 0 6 1
1
<?php
2
3
namespace Maiorano\Shortcodes\Manager;
4
5
use Maiorano\Shortcodes\Contracts\AliasInterface;
6
use Maiorano\Shortcodes\Contracts\AttributeInterface;
7
use Maiorano\Shortcodes\Contracts\ContainerAwareInterface;
8
use Maiorano\Shortcodes\Contracts\ShortcodeInterface;
9
use Maiorano\Shortcodes\Exceptions\DeregisterException;
10
use Maiorano\Shortcodes\Exceptions\RegisterException;
11
use Maiorano\Shortcodes\Parsers\DefaultParser;
12
use Maiorano\Shortcodes\Parsers\ParserInterface;
13
14
/**
15
 * Class ShortcodeManager.
16
 */
17
final class ShortcodeManager extends BaseManager
18
{
19
    /**
20
     * @var ParserInterface
21
     */
22
    protected $parser;
23
24
    /**
25
     * ShortcodeManager constructor.
26
     *
27
     * @param ShortcodeInterface[] $shortcodes
28
     * @param ParserInterface|null $parser
29
     *
30
     * @throws RegisterException
31
     */
32 23
    public function __construct(array $shortcodes = [], ParserInterface $parser = null)
33
    {
34 23
        $this->parser = $parser ?? new DefaultParser();
35 23
        $this->registerAll($shortcodes);
36 23
    }
37
38
    /**
39
     * @param ShortcodeInterface $shortcode
40
     * @param string|null        $name
41
     *
42
     * @throws RegisterException
43
     *
44
     * @return ManagerInterface
45
     */
46 19
    public function register(ShortcodeInterface $shortcode, ?string $name = null): ManagerInterface
47
    {
48 19
        parent::register($shortcode, $name);
49 18
        if ($shortcode instanceof AliasInterface) {
50 6
            foreach ($shortcode->getAlias() as $alias) {
51 5
                if (!$this->isRegistered($alias)) {
52 5
                    parent::register($shortcode, $alias);
53
                }
54
            }
55
        }
56
57 18
        return $this;
58
    }
59
60
    /**
61
     * @param ShortcodeInterface[] $shortcodes
62
     *
63
     * @throws RegisterException
64
     *
65
     * @return ManagerInterface
66
     */
67 23
    public function registerAll(array $shortcodes): ManagerInterface
68
    {
69 23
        foreach ($shortcodes as $k => $s) {
70 3
            $this->register($s, $k);
71
        }
72
73 23
        return $this;
74
    }
75
76
    /**
77
     * @param string $name
78
     * @param bool   $includeAlias
79
     *
80
     * @throws DeregisterException
81
     *
82
     * @return ManagerInterface
83
     */
84 6
    public function deregister(string $name, bool $includeAlias = true): ManagerInterface
85
    {
86 6
        $shortcode = $this->shortcodes[$name] ?? false;
87 6
        if ($shortcode && $shortcode instanceof AliasInterface) {
88 4
            if ($name === $shortcode->getName() && $includeAlias) {
89 2
                foreach ($shortcode->getAlias() as $alias) {
90 2
                    parent::deregister($alias);
91
                }
92
            }
93
        }
94
95 6
        return parent::deregister($name);
96
    }
97
98
    /**
99
     * @param string $name
100
     * @param string $alias
101
     *
102
     * @throws RegisterException
103
     *
104
     * @return ManagerInterface
105
     */
106 3
    public function alias(string $name, string $alias): ManagerInterface
107
    {
108 3
        if (!$this->isRegistered($name)) {
109 1
            throw RegisterException::missing($name);
110
        }
111 2
        if ($this[$name] instanceof AliasInterface) {
112 1
            $this[$name]->alias($alias);
0 ignored issues
show
Bug introduced by
The method alias() does not exist on Maiorano\Shortcodes\Contracts\ShortcodeInterface. It seems like you code against a sub-type of Maiorano\Shortcodes\Contracts\ShortcodeInterface such as Maiorano\Shortcodes\Library\Ipsum or Maiorano\Shortcodes\Contracts\AliasInterface or Maiorano\Shortcodes\Library\SimpleShortcode or Maiorano\Shortcodes\Library\SimpleShortcode or anonymous//tests/Unit/Contracts/AliasTest.php$1. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
            $this[$name]->/** @scrutinizer ignore-call */ 
113
                          alias($alias);
Loading history...
113
        } else {
114 1
            throw RegisterException::noAlias();
115
        }
116
117 1
        if (!$this[$name] instanceof ContainerAwareInterface) {
118 1
            parent::register($this[$name], $alias);
119
        }
120
121 1
        return $this;
122
    }
123
124
    /**
125
     * @param string          $content
126
     * @param string|string[] $tags
127
     *
128
     * @return bool
129
     */
130 2
    public function hasShortcode(string $content, $tags = []): bool
131
    {
132 2
        $tags = $this->preProcessTags($tags);
133 2
        $matches = $this->parser->parseShortcode($content, $tags);
134
135 2
        return !empty($matches);
136
    }
137
138
    /**
139
     * @param string          $content
140
     * @param string|string[] $tags
141
     * @param bool            $deep
142
     *
143
     * @return string
144
     */
145 3
    public function doShortcode(string $content, $tags = [], bool $deep = false): string
146
    {
147 3
        $tags = $this->preProcessTags($tags);
148
        $handler = function (string $tag, ?string $content = null, array $atts = []) {
149 2
            $shortcode = $this[$tag];
150
151 2
            if ($shortcode instanceof AttributeInterface) {
152 1
                $atts = array_merge($shortcode->getAttributes(), $atts);
153
154 1
                return $shortcode->handle($content, $atts);
155
            }
156
157 1
            return $shortcode->handle($content);
158 3
        };
159
160 3
        $result = (string) $this->parser->parseShortcode($content, $tags, $handler);
161
162 3
        if ($deep && $this->hasShortcode($result, $tags)) {
163 1
            return $this->doShortcode($result, $tags, $deep);
164
        }
165
166 3
        return $result;
167
    }
168
169
    /**
170
     * @param string|string[] $tags
171
     *
172
     * @return string[]
173
     */
174 4
    private function preProcessTags($tags): array
175
    {
176 4
        if (!$tags) {
177 4
            return $this->getRegistered();
178
        }
179
180 2
        if (is_string($tags)) {
181 1
            $tags = explode('|', $tags);
182
        }
183
184 2
        return array_filter($tags, [$this, 'isRegistered']);
185
    }
186
}
187