ContainerAware::getManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 5
    public function bind(ManagerInterface $manager): void
27
    {
28 5
        $this->manager = $manager;
29 5
    }
30
31
    /**
32
     * @return bool
33
     */
34 8
    public function isBound(): bool
35
    {
36 8
        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 2
    public function hasShortcode(string $content): bool
51
    {
52 2
        if (!($this->isBound())) {
53 1
            throw RegisterException::missing($this->name);
54
        }
55
56 1
        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 2
    public function doShortcode(string $content, bool $deep = false): string
72
    {
73 2
        if (!($this->isBound())) {
74 1
            throw RegisterException::missing($this->name);
75
        }
76
77 1
        return $this->manager->doShortcode($content, $this->getContext(), $deep);
78
    }
79
80
    /**
81
     * @return ManagerInterface
82
     */
83 2
    public function getManager(): ManagerInterface
84
    {
85 2
        return $this->manager;
86
    }
87
88
    /**
89
     * Utility method.
90
     *
91
     * @return string[]
92
     */
93 2
    private function getContext(): array
94
    {
95 2
        $context = [$this->name];
96 2
        if ($this instanceof AliasInterface) {
97 2
            $context = array_unique(array_merge($context, $this->getAlias()));
98
        }
99
100 2
        return $context;
101
    }
102
}
103