SimpleShortcode   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 19
dl 0
loc 69
ccs 15
cts 15
cp 1
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 8 2
A alias() 0 8 2
A __construct() 0 5 1
1
<?php
2
3
namespace Maiorano\Shortcodes\Library;
4
5
use Closure;
6
use Maiorano\Shortcodes\Contracts;
7
use Maiorano\Shortcodes\Contracts\Traits;
8
use Maiorano\Shortcodes\Exceptions\RegisterException;
9
10
/**
11
 * Creation of Shortcodes programmatically.
12
 */
13
final class SimpleShortcode implements Contracts\AttributeInterface, Contracts\AliasInterface, Contracts\ContainerAwareInterface
14
{
15
    use Traits\Attribute;
16
    use Traits\Alias;
17
    use Traits\ContainerAware;
18
19
    /**
20
     * @var string
21
     */
22
    protected $name;
23
24
    /**
25
     * @var mixed[]
26
     */
27
    protected $attributes;
28
29
    /**
30
     * @var string[]
31
     */
32
    protected $alias = [];
33
34
    /**
35
     * @var Closure|null
36
     */
37
    protected $callback;
38
39
    /**
40
     * @param string       $name
41
     * @param mixed[]|null $atts
42
     * @param Closure|null $callback
43
     */
44 10
    public function __construct($name, $atts = [], Closure $callback = null)
45
    {
46 10
        $this->name = $name;
47 10
        $this->attributes = (array) $atts;
48 10
        $this->callback = $callback;
49 10
    }
50
51
    /**
52
     * @param string|null $content
53
     * @param mixed[]     $atts
54
     *
55
     * @return string
56
     */
57 2
    public function handle(?string $content = null, array $atts = []): string
58
    {
59 2
        if (is_null($this->callback)) {
60 1
            return (string) $content;
61
        }
62 1
        $callback = $this->callback->bindTo($this, $this);
63
64 1
        return $callback($content, $atts);
65
    }
66
67
    /**
68
     * @param string $alias
69
     *
70
     * @throws RegisterException
71
     *
72
     * @return Contracts\AliasInterface
73
     */
74 1
    public function alias(string $alias): Contracts\AliasInterface
75
    {
76 1
        if (!in_array($alias, $this->alias)) {
77 1
            $this->alias[] = $alias;
78
        }
79 1
        $this->aliasHelper($alias);
80
81 1
        return $this;
82
    }
83
}
84