Passed
Branch version-bump (36e4b3)
by Matt
04:39
created

ContainerAware::getManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Maiorano\Shortcodes\Contracts\Traits;
4
5
use Maiorano\Shortcodes\Contracts\AliasInterface;
6
use Maiorano\Shortcodes\Exceptions\RegisterException;
7
use Maiorano\Shortcodes\Manager\ManagerInterface;
8
9
/**
10
 * Trait ContainerAware
11
 * Assists in satisfying the ContainerAwareInterface requirements
12
 * Exposes the management container and its public members.
13
 */
14
trait ContainerAware
15
{
16
    /**
17
     * @var ManagerInterface
18
     */
19
    protected $manager;
20
21
    /**
22
     * @param ManagerInterface $manager
23
     *
24
     * @return void
25
     */
26
    public function bind(ManagerInterface $manager): void
27
    {
28
        $this->manager = $manager;
29
    }
30
31
    /**
32
     * @return bool
33
     */
34
    public function isBound(): bool
35
    {
36
        return $this->manager instanceof ManagerInterface;
37
    }
38
39
    /**
40
     * Convenience method
41
     * Utilizes manager's implementation of hasShortcode
42
     * Limits search to this shortcode's context.
43
     *
44
     * @param string $content
45
     *
46
     * @throws RegisterException
47
     *
48
     * @return bool
49
     */
50
    public function hasShortcode(string $content): bool
51
    {
52
        if (!($this->isBound())) {
53
            throw RegisterException::missing($this->name);
54
        }
55
56
        return $this->manager->hasShortcode($content, $this->getContext());
57
    }
58
59
    /**
60
     * Convenience method
61
     * Utilizes manager's implementation of doShortcode
62
     * Limits search to this shortcode's context.
63
     *
64
     * @param string $content
65
     * @param bool   $deep
66
     *
67
     * @throws RegisterException
68
     *
69
     * @return string
70
     */
71
    public function doShortcode(string $content, bool $deep = false): string
72
    {
73
        if (!($this->isBound())) {
74
            throw RegisterException::missing($this->name);
75
        }
76
77
        return $this->manager->doShortcode($content, $this->getContext(), $deep);
78
    }
79
80
    /**
81
     * @return ManagerInterface
82
     */
83
    public function getManager(): ManagerInterface
84
    {
85
        return $this->manager;
86
    }
87
88
    /**
89
     * Utility method.
90
     *
91
     * @return array
92
     */
93
    private function getContext(): array
94
    {
95
        $context = [$this->name];
96
        if ($this instanceof AliasInterface) {
97
            $context = array_unique(array_merge($context, $this->getAlias()));
98
        }
99
100
        return $context;
101
    }
102
}
103