Completed
Push — master ( 5bfa1a...da88de )
by Joschi
03:40
created

ApparatObjectTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.32%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 110
ccs 28
cts 31
cp 0.9032
rs 10
wmc 10
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetExists() 0 4 1
A offsetGet() 0 15 2
A offsetSet() 0 4 1
A offsetUnset() 0 7 1
A delegateObjectGetter() 0 12 2
A __call() 0 15 3
1
<?php
2
3
/**
4
 * apparat/object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Infrastructure
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Infrastructure\Model\Object\Apparat\Traits;
38
39
use Apparat\Object\Application\Model\Object\ApplicationObjectInterface;
40
use Apparat\Object\Ports\Exceptions\InvalidArgumentException;
41
42
/**
43
 * Apparat object trait
44
 *
45
 * @package Apparat\Object
46
 * @subpackage Apparat\Object\Infrastructure
47
 * @property ApplicationObjectInterface $object
48
 */
49
trait ApparatObjectTrait
50
{
51
    /**
52
     * Property mapping
53
     *
54
     * @var array
55
     */
56
    protected $mapping = [];
57
58
    /**
59
     * Generic getter
60
     *
61
     * @param string $method Method name
0 ignored issues
show
Bug introduced by
There is no parameter named $method. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
62
     * @param array $arguments Arguments
63
     * @throws \BadMethodCallException If the method is unknown
64
     */
65 1
    public function __call($name, array $arguments)
66
    {
67
        // If a getter was called
68 1
        if (!strncmp('get', $name, 3)) {
69 1
            $property = lcfirst(substr($name, 3));
70 1
            if (array_key_exists($property, $this->mapping)) {
71 1
                $arguments = (array)$this->mapping[$property];
72 1
                $getter = 'get'.ucfirst(array_shift($arguments));
73 1
                return $this->delegateObjectGetter($property, $getter, $arguments);
74
            }
75
        }
76
77
        // If the method is unknown
78 1
        throw new \BadMethodCallException(sprintf('Unknown apparat object method "%s()"', $name));
79
    }
80
81
    /**
82
     * Return whether a particular property exists
83
     *
84
     * @param string $offset Property name
85
     */
86 1
    public function offsetExists($offset)
87
    {
88 1
        return array_key_exists($offset, $this->mapping);
89
    }
90
91
    /**
92
     * Return a particular property
93
     *
94
     * @param string $offset Property name
95
     * @return mixed Property value
96
     */
97 2
    public function offsetGet($offset)
98
    {
99
        // If a known object property has been requested
100 2
        if (array_key_exists($offset, $this->mapping)) {
101 2
            $arguments = (array)$this->mapping[$offset];
102 2
            $property = array_shift($arguments);
103 2
            $getter = 'get'.ucfirst($property);
104 2
            return $this->delegateObjectGetter($property, $getter, $arguments);
105
        }
106
107
        throw new InvalidArgumentException(
108
            sprintf('Invalid apparat object property "%s"', $offset),
109
            InvalidArgumentException::INVALID_APPARAT_OBJECT_PROPERTY
110
        );
111
    }
112
113
    /**
114
     * Set a particular property
115
     *
116
     * @param string $offset Property name
117
     * @param mixed $value Property value
118
     */
119 1
    public function offsetSet($offset, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $offset is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
    {
121
        // TODO: Implement offsetSet() method.
122 1
    }
123
124
    /**
125
     * Unset a particular property
126
     *
127
     * @param string $offset Property name
128
     */
129 1
    public function offsetUnset($offset)
130
    {
131 1
        throw new InvalidArgumentException(
132 1
            sprintf('Cannot unset apparat object property "%s"', $offset),
133 1
            InvalidArgumentException::CANNOT_UNSET_APPARAT_OBJECT_PROPERTY
134
        );
135
    }
136
137
    /**
138
     * Delegate the mapped object getter
139
     *
140
     * @param string $property Property name
141
     * @param string $getter Getter name
142
     * @param array $arguments Getter arguments
143
     * @return mixed Property value
144
     * @throws InvalidArgumentException If the property is invalid
145
     */
146 2
    protected function delegateObjectGetter($property, $getter, array $arguments)
147
    {
148
        // If the property is invalid
149 2
        if (!is_callable([$this->object, $getter])) {
150 1
            throw new InvalidArgumentException(
151 1
                sprintf('Invalid apparat object property "%s"', $property),
152 1
                InvalidArgumentException::INVALID_APPARAT_OBJECT_PROPERTY
153
            );
154
        }
155
156 1
        return $this->object->$getter(...$arguments);
157
    }
158
}
159