Completed
Push — v2 ( 88b419...d5d6a3 )
by Joschi
05:06
created

Item   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 31.43%

Importance

Changes 0
Metric Value
dl 0
loc 121
ccs 11
cts 35
cp 0.3143
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B isOfType() 0 20 6
A __get() 0 4 1
A firstOf() 0 12 2
A firstOfPropertyNames() 0 12 3
A getPropertyValueOrValueList() 0 4 1
A getPropertyValues() 0 8 2
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\Infrastructure\Factory\ProfiledNameFactory;
42
use Jkphl\Micrometa\Ports\Exceptions\InvalidArgumentException;
43
44
/**
45
 * Micro information item
46
 *
47
 * @package Jkphl\Micrometa
48
 * @subpackage Jkphl\Micrometa\Ports
49
 */
50
class Item implements ItemInterface
51
{
52
    /**
53
     * Application item
54
     *
55
     * @var ApplicationItemInterface
56
     */
57
    protected $item;
58
59
    /**
60
     * Item constructor
61
     *
62
     * @param ApplicationItemInterface $item Application item
63
     */
64 2
    public function __construct(ApplicationItemInterface $item)
65
    {
66 2
        $this->item = $item;
67 2
    }
68
69
    /**
70
     * Return whether the item is of a particular type (or contained in a list of types)
71
     *
72
     * @param string $name Name
73
     * @param string|null $profile Profile
74
     * @return boolean Item type is contained in the list of types
75
     */
76 1
    public function isOfType($name, $profile = null)
77
    {
78 1
        $types = ProfiledNameFactory::createFromArguments(func_get_args());
79
80
        // Run through all item types
81
        /** @var \stdClass $itemType */
82 1
        foreach ($this->item->getType() as $itemType) {
83
            // Run through all query types
84
            /** @var \stdClass $queryType */
85 1
            foreach ($types as $queryType) {
86 1
                if (($queryType->name == $itemType->name) &&
87 1
                    (($queryType->profile === null) ? true : ($queryType->profile == $itemType->profile))
88 1
                ) {
89 1
                    return true;
90
                }
91
            }
92
        }
93
94
        return false;
95
    }
96
97
    /**
98
     * Get the values or first value of an item property
99
     *
100
     * Prepend the property name with an "s" to retrieve the list of all available property values.
101
     *
102
     * @param string $name Item property name
103
     * @return string Values or first value of an item property
104
     */
105
    public function __get($name)
106
    {
107
        // TODO: Implement __get() method.
108
    }
109
110
    /**
111
     * Get all values or the first value for a particular property (in a property list)
112
     *
113
     * Append the property names with an "s" to retrieve the list of all available property values.
114
     *
115
     * @param array ...$names Property names
116
     * @return string|string[] Property value(s)
117
     * @throws InvalidArgumentException If no property name was given
118
     */
119
    public function firstOf(...$names)
120
    {
121
        // If no property name was given
122
        if (!count($names)) {
123
            throw new InvalidArgumentException(
124
                sprintf(InvalidArgumentException::MISSING_PROPERTY_NAME_STR, __CLASS__.'::'.__METHOD__),
125
                InvalidArgumentException::MISSING_PROPERTY_NAME
126
            );
127
        }
128
129
        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 129 which is incompatible with the return type declared by the interface Jkphl\Micrometa\Ports\Item\ItemInterface::firstOf of type string|string[].
Loading history...
130
    }
131
132
    /**
133
     * Return the first non-NULL value of a property list
134
     *
135
     * @param array $names Property names
136
     * @return string|array|null First existing property name
137
     */
138
    protected function firstOfPropertyNames(array $names)
139
    {
140
        // Run through all property names
141
        foreach ($names as $name) {
142
            $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...
143
            if ($value !== null) {
144
                return $value;
145
            }
146
        }
147
148
        return null;
149
    }
150
151
    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...
152
    {
153
        return null;
154
    }
155
156
    /**
157
     * Return the values of a particular property
158
     *
159
     * @param string $name Property name
160
     * @return array|null Property values
161
     */
162
    protected function getPropertyValues($name)
163
    {
164
        try {
165
            return $this->item->getProperty($name);
166
        } catch (OutOfBoundsException $e) {
167
            return null;
168
        }
169
    }
170
}
171