ExtensionAwareTrait::addExtension()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.2
cc 4
eloc 12
nc 3
nop 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Extension;
16
17
use Phossa2\Shared\Message\Message;
18
use Phossa2\Shared\Exception\LogicException;
19
use Phossa2\Shared\Exception\BadMethodCallException;
20
21
/**
22
 * ExtensionAwareTrait
23
 *
24
 * @package Phossa2\Shared
25
 * @author  Hong Zhang <[email protected]>
26
 * @see     ExtensionAwareInterface
27
 * @version 2.0.23
28
 * @since   2.0.23 added
29
 */
30
trait ExtensionAwareTrait
31
{
32
    /**
33
     * Methods provided by extensions
34
     *
35
     * @var    array
36
     * @access protected
37
     */
38
    protected $extension_methods = [];
39
40
    /**
41
     * @param  string $method
42
     * @param  array $arguments
43
     * @return mixed
44
     * @throws BadMethodCallException if method not found
45
     * @access public
46
     */
47
    public function __call($method, array $arguments)
48
    {
49
        if ($this->hasExtensionMethod($method)) {
50
            return $this->runExtension($method, $arguments);
51
        }
52
        throw new BadMethodCallException(
53
            Message::get(Message::MSG_METHOD_NOTFOUND, $method),
54
            Message::MSG_METHOD_NOTFOUND
55
        );
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    public function addExtension(
62
        ExtensionInterface $ext,
63
        /*# bool */ $forceOverride = false
64
    ) {
65
        foreach ($ext->methodsAvailable() as $method) {
66
            if (isset($this->extension_methods[$method]) &&
67
                !$forceOverride) {
68
                throw new LogicException(
69
                    Message::get(Message::MSG_EXTENSION_METHOD, $method),
70
                    Message::MSG_EXTENSION_METHOD
71
                );
72
            }
73
            $this->extension_methods[$method] = $ext;
74
        }
75
        $ext->boot($this);
76
        return $this;
77
    }
78
79
    /**
80
     * Is this method from extensions ?
81
     *
82
     * @param  string $method
83
     * @return bool
84
     * @access protected
85
     */
86
    protected function hasExtensionMethod(/*# string */ $method)/*# : bool */
87
    {
88
        return isset($this->extension_methods[$method]);
89
    }
90
91
    /**
92
     * Include this in server's __call()
93
     *
94
     * @param  string $method
95
     * @param  array $args
96
     * @return mixed
97
     * @access protected
98
     */
99
    protected function runExtension(/*# string */ $method, array $args)
100
    {
101
        /* @var $ext ExtensionInterface */
102
        $ext = $this->extension_methods[$method];
103
104
        // execute extesion method
105
        return call_user_func_array([$ext, $method], $args);
106
    }
107
}
108