BaseManager   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 27
c 1
b 0
f 0
dl 0
loc 143
ccs 36
cts 36
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetSet() 0 4 3
A offsetUnset() 0 3 1
A isRegistered() 0 3 1
A offsetGet() 0 7 2
A register() 0 17 5
A deregister() 0 11 3
A getIterator() 0 3 1
A getRegistered() 0 3 1
A offsetExists() 0 3 1
1
<?php
2
3
namespace Maiorano\Shortcodes\Manager;
4
5
use ArrayAccess;
6
use ArrayIterator;
7
use IteratorAggregate;
8
use Maiorano\Shortcodes\Contracts\ContainerAwareInterface;
9
use Maiorano\Shortcodes\Contracts\ShortcodeInterface;
10
use Maiorano\Shortcodes\Exceptions\DeregisterException;
11
use Maiorano\Shortcodes\Exceptions\RegisterException;
12
13
/**
14
 * Class BaseManager.
15
 *
16
 * @implements ArrayAccess<string, ShortcodeInterface>
17
 * @implements IteratorAggregate<string, ShortcodeInterface>
18
 */
19
abstract class BaseManager implements ManagerInterface, ArrayAccess, IteratorAggregate
20
{
21
    /**
22
     * @var ShortcodeInterface[]
23
     */
24
    protected $shortcodes = [];
25
26
    /**
27
     * @param ShortcodeInterface $shortcode
28
     * @param string|null        $name
29
     *
30
     * @throws RegisterException
31
     *
32
     * @return ManagerInterface
33
     */
34 19
    public function register(ShortcodeInterface $shortcode, ?string $name = null): ManagerInterface
35
    {
36 19
        $name = $name ?: $shortcode->getName();
37
38 19
        if (!$name) {
39 1
            throw RegisterException::blank();
40 18
        } elseif ($this->isRegistered($name)) {
41 1
            throw RegisterException::duplicate($name);
42
        }
43
44 18
        if ($shortcode instanceof ContainerAwareInterface) {
45 1
            $shortcode->bind($this);
46
        }
47
48 18
        $this->shortcodes[$name] = $shortcode;
49
50 18
        return $this;
51
    }
52
53
    /**
54
     * @param string $name
55
     *
56
     * @throws DeregisterException
57
     *
58
     * @return ManagerInterface
59
     */
60 6
    public function deregister(string $name): ManagerInterface
61
    {
62 6
        if (!$name) {
63 1
            throw DeregisterException::blank();
64 5
        } elseif (!$this->isRegistered($name)) {
65 1
            throw DeregisterException::missing($name);
66
        }
67
68 4
        unset($this->shortcodes[$name]);
69
70 4
        return $this;
71
    }
72
73
    /**
74
     * @param string $name
75
     *
76
     * @return bool
77
     */
78 21
    public function isRegistered(string $name): bool
79
    {
80 21
        return isset($this->shortcodes[$name]);
81
    }
82
83
    /**
84
     * @return string[]
85
     */
86 5
    public function getRegistered(): array
87
    {
88 5
        return array_keys($this->shortcodes);
89
    }
90
91
    /**
92
     * @param string   $content
93
     * @param string[] $tags
94
     *
95
     * @return bool
96
     */
97
    abstract public function hasShortcode(string $content, $tags = []): bool;
98
99
    /**
100
     * @param string   $content
101
     * @param string[] $tags
102
     * @param bool     $deep
103
     *
104
     * @return string
105
     */
106
    abstract public function doShortcode(string $content, $tags = [], bool $deep = false): string;
107
108
    /**
109
     * @param string $offset
110
     *
111
     * @throws RegisterException
112
     *
113
     * @return ShortcodeInterface
114
     */
115 7
    public function offsetGet($offset): ShortcodeInterface
116
    {
117 7
        if (!$this->isRegistered($offset)) {
118 1
            throw RegisterException::missing($offset);
119
        }
120
121 6
        return $this->shortcodes[$offset];
122
    }
123
124
    /**
125
     * @param string $offset
126
     * @param mixed  $value
127
     *
128
     * @throws RegisterException
129
     */
130 12
    public function offsetSet($offset, $value): void
131
    {
132 12
        $name = is_string($offset) && !is_numeric($offset) ? $offset : null;
133 12
        $this->register($value, $name);
134 12
    }
135
136
    /**
137
     * @param mixed $offset
138
     *
139
     * @throws DeregisterException
140
     */
141 1
    public function offsetUnset($offset): void
142
    {
143 1
        $this->deregister($offset);
144 1
    }
145
146
    /**
147
     * @param mixed $offset
148
     *
149
     * @return bool
150
     */
151 6
    public function offsetExists($offset): bool
152
    {
153 6
        return $this->isRegistered($offset);
154
    }
155
156
    /**
157
     * @return ArrayIterator<string, ShortcodeInterface>
158
     */
159 7
    public function getIterator(): ArrayIterator
160
    {
161 7
        return new ArrayIterator($this->shortcodes);
162
    }
163
}
164