Completed
Push — v2 ( a2e732...ad2ba8 )
by Joschi
07:14
created

Item::getPropertyValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\Micrometa\Ports
9
 * @author Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright Copyright © 2017 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 © 2017 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 Jkphl\Micrometa\Ports\Item;
38
39
use Jkphl\Micrometa\Application\Item\ItemInterface as ApplicationItemInterface;
40
use Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException;
41
use Jkphl\Micrometa\Ports\Exceptions\InvalidArgumentException;
42
43
/**
44
 * Micro information item
45
 *
46
 * @package Jkphl\Micrometa
47
 * @subpackage Jkphl\Micrometa\Ports
48
 */
49
class Item implements ItemInterface
50
{
51
    /**
52
     * Application item
53
     *
54
     * @var ApplicationItemInterface
55
     */
56
    protected $item;
57
58
    /**
59
     * Item constructor
60
     *
61
     * @param ApplicationItemInterface $item Application item
62
     */
63 1
    public function __construct(ApplicationItemInterface $item)
64
    {
65 1
        $this->item = $item;
66 1
    }
67
68
    /**
69
     * Return whether the item is of a particular type (or contained in a list of types)
70
     *
71
     * @param array ...$types Item types
72
     * @return boolean Item is contained in the list of types
73
     * @throws InvalidArgumentException If no item type was given
74
     */
75 1
    public function isOfType(...$types)
76
    {
77
        // If no item type was given
78 1
        if (!count($types)) {
79
            throw new InvalidArgumentException(
80
                sprintf(InvalidArgumentException::MISSING_ITEM_TYPE_STR, __CLASS__.'::'.__METHOD__),
81
                InvalidArgumentException::MISSING_ITEM_TYPE
82
            );
83
        }
84
85 1
        return count(array_intersect($types, $this->item->getType())) > 0;
86
    }
87
88
    /**
89
     * Get the values or first value of an item property
90
     *
91
     * Prepend the property name with an "s" to retrieve the list of all available property values.
92
     *
93
     * @param string $name Item property name
94
     * @return string Values or first value of an item property
95
     */
96
    public function __get($name)
97
    {
98
        // TODO: Implement __get() method.
99
    }
100
101
    /**
102
     * Get all values or the first value for a particular property (in a property list)
103
     *
104
     * Append the property names with an "s" to retrieve the list of all available property values.
105
     *
106
     * @param array ...$names Property names
107
     * @return string|string[] Property value(s)
108
     * @throws InvalidArgumentException If no property name was given
109
     */
110
    public function firstOf(...$names)
111
    {
112
        // If no property name was given
113
        if (!count($names)) {
114
            throw new InvalidArgumentException(
115
                sprintf(InvalidArgumentException::MISSING_PROPERTY_NAME_STR, __CLASS__.'::'.__METHOD__),
116
                InvalidArgumentException::MISSING_PROPERTY_NAME
117
            );
118
        }
119
120
        return $this->firstOfPropertyNames($names);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->firstOfPropertyNames($names); of type string|array|null adds the type array to the return on line 120 which is incompatible with the return type declared by the interface Jkphl\Micrometa\Ports\Item\ItemInterface::firstOf of type string|string[].
Loading history...
121
    }
122
123
    /**
124
     * Return the first non-NULL value of a property list
125
     *
126
     * @param array $names Property names
127
     * @return string|array|null First existing property name
128
     */
129
    protected function firstOfPropertyNames(array $names)
130
    {
131
        // Run through all property names
132
        foreach ($names as $name) {
133
            $value = $this->getPropertyValueOrValueList($name);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $value is correct as $this->getPropertyValueOrValueList($name) (which targets Jkphl\Micrometa\Ports\It...pertyValueOrValueList()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
134
            if ($value !== null) {
135
                return $value;
136
            }
137
        }
138
139
        return null;
140
    }
141
142
    protected function getPropertyValueOrValueList($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name 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...
143
    {
144
        return null;
145
    }
146
147
    /**
148
     * Return the values of a particular property
149
     *
150
     * @param string $name Property name
151
     * @return array|null Property values
152
     */
153
    protected function getPropertyValues($name)
154
    {
155
        try {
156
            return $this->item->getProperty($name);
157
        } catch (OutOfBoundsException $e) {
158
            return null;
159
        }
160
    }
161
}
162