Passed
Push — master ( a3ff66...3ad8df )
by Matt
01:45
created

ShortcodeManager::alias()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.9666
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4
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
class ShortcodeManager extends BaseManager
18
{
19
    /**
20
     * @var DefaultParser|ParserInterface
21
     */
22
    protected $parser;
23
24
    /**
25
     * ShortcodeManager constructor.
26
     *
27
     * @param array                $shortcodes
28
     * @param ParserInterface|null $parser
29
     */
30 23
    public function __construct(array $shortcodes = [], ParserInterface $parser = null)
31
    {
32 23
        $this->parser = $parser ?? new DefaultParser();
33 23
        $this->registerAll($shortcodes);
34 23
    }
35
36
    /**
37
     * @param ShortcodeInterface $shortcode
38
     * @param string|null        $name
39
     *
40
     * @throws RegisterException
41
     *
42
     * @return static
43
     */
44 19
    public function register(ShortcodeInterface $shortcode, ?string $name = null): ManagerInterface
45
    {
46 19
        parent::register($shortcode, $name);
47 18
        if ($shortcode instanceof AliasInterface) {
48 6
            foreach ($shortcode->getAlias() as $alias) {
49 5
                if (!$this->isRegistered($alias)) {
50 5
                    parent::register($shortcode, $alias);
51
                }
52
            }
53
        }
54
55 18
        return $this;
56
    }
57
58
    /**
59
     * @param array $shortcodes
60
     *
61
     * @return static
62
     */
63 23
    public function registerAll(array $shortcodes): ManagerInterface
64
    {
65 23
        foreach ($shortcodes as $k => $s) {
66 3
            $this[$k] = $s;
67
        }
68
69 23
        return $this;
70
    }
71
72
    /**
73
     * @param string $name
74
     * @param bool   $includeAlias
75
     *
76
     * @throws DeregisterException
77
     *
78
     * @return static
79
     */
80 6
    public function deregister(string $name, bool $includeAlias = true): ManagerInterface
81
    {
82 6
        $shortcode = $this->shortcodes[$name] ?? false;
83 6
        if ($shortcode && $shortcode instanceof AliasInterface) {
84 4
            if ($name === $shortcode->getName() && $includeAlias) {
85 2
                foreach ($shortcode->getAlias() as $alias) {
86 2
                    parent::deregister($alias);
87
                }
88
            }
89
        }
90
91 6
        return parent::deregister($name);
92
    }
93
94
    /**
95
     * @param string $name
96
     * @param string $alias
97
     *
98
     * @throws RegisterException
99
     *
100
     * @return static
101
     */
102 3
    public function alias(string $name, string $alias): ManagerInterface
103
    {
104 3
        if (!$this->isRegistered($name)) {
105 1
            throw RegisterException::missing($name);
106
        }
107 2
        if ($this[$name] instanceof AliasInterface) {
108 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

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