Completed
Push — master ( 5ac40d...779f6f )
by Zoltán
11:49
created

AbstractExtensionReceiver::getAllLoadedExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace BuildR\Utils\Extension;
2
3
use BuildR\Foundation\Exception\InvalidArgumentException;
4
5
/**
6
 * This object represents a string in OO format and allow nifty manipulation
7
 * and analysis of the provided string.
8
 *
9
 * BuildR PHP Framework
10
 *
11
 * @author Zoltán Borsos <[email protected]>
12
 * @package Utils
13
 * @subpackage Extension
14
 *
15
 * @copyright    Copyright 2016, Zoltán Borsos.
16
 * @license      https://github.com/BuildrPHP/Utils/blob/master/LICENSE.md
17
 * @link         https://github.com/BuildrPHP/Utils
18
 */
19
abstract class AbstractExtensionReceiver implements ExtensionReceiverInterface {
20
21
    /**
22
     * @type \BuildR\Utils\Extension\ObjectExtensionInterface[]
23
     */
24
    protected $extensions = [];
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function __call($name, $arguments) {
30
        foreach($this->extensions as $extension) {
31
            if($extension->hasMethod($name)) {
32
                $result = $extension->runMethod($name, $this->getCurrentValue(), $arguments);
33
                return $this->processResult($result);
34
            }
35
        }
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    public function loadExtension(ObjectExtensionInterface $extension) {
42
        $type = $this->shouldReceiveType();
43
44
        if(!($extension instanceof $type)) {
45
            $message = 'This object only take ' . $type . ' extensions! Given type: ' . gettype($extension);
46
            throw new InvalidArgumentException($message);
47
        }
48
49
        $extClass = get_class($extension);
50
        $this->extensions[$extClass] = $extension;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function isExtensionLoaded(ObjectExtensionInterface $extension) {
57
        return array_key_exists(get_class($extension), $this->extensions);
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function getAllLoadedExtension() {
64
        return array_keys($this->extensions);
65
    }
66
67
}
68