Completed
Push — master ( bf8da1...e4b361 )
by Zoltán
02:31
created

AbstractExtensionReceiver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.74%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 53
ccs 18
cts 19
cp 0.9474
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 8 3
A loadExtension() 0 15 3
A isExtensionLoaded() 0 3 1
A getAllLoadedExtension() 0 3 1
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 2
    public function __call($name, $arguments) {
30 2
        foreach($this->extensions as $extension) {
31 2
            if($extension->hasMethod($name)) {
32 2
                $result = $extension->runMethod($name, $this->getCurrentValue(), $arguments);
33 2
                return $this->processResult($result);
34
            }
35
        }
36
    } //@codeCoverageIgnore
37
38
    /**
39
     * {@inheritDoc}
40
     */
41 4
    public function loadExtension(ObjectExtensionInterface $extension) {
42 4
        if($this->isExtensionLoaded($extension)) {
43
            return; //@codeCoverageIgnore
44
        }
45
46 4
        $type = $this->shouldReceiveType();
47 4
        $extClass = get_class($extension);
48
49 4
        if(!($extension instanceof $type)) {
50 1
            $message = 'This object only take ' . $type . ' extensions! Given: ' . $extClass;
51 1
            throw new InvalidArgumentException($message);
52
        }
53
54 3
        $this->extensions[$extClass] = $extension;
55 3
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60 4
    public function isExtensionLoaded(ObjectExtensionInterface $extension) {
61 4
        return array_key_exists(get_class($extension), $this->extensions);
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 1
    public function getAllLoadedExtension() {
68 1
        return array_keys($this->extensions);
69
    }
70
71
}
72