Completed
Pull Request — master (#55)
by Tim
04:51
created

RegistryProcessor::raiseCounter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Services\RegistryProcessor
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Services;
22
23
/**
24
 * Array based implementation of a registry.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2016 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import
30
 * @link      http://www.techdivision.com
31
 */
32
class RegistryProcessor implements RegistryProcessorInterface
33
{
34
35
    /**
36
     * A storage for the attributes.
37
     *
38
     * @var array
39
     */
40
    protected $attributes = array();
41
42
    /**
43
     * Register the passed attribute under the specified key in the registry.
44
     *
45
     * @param mixed $key   The key to register the value with
46
     * @param mixed $value The value to be registered
47
     *
48
     * @return void
49
     * @throws \Exception Is thrown, if the key has already been used
50
     */
51
    public function setAttribute($key, $value)
52
    {
53
54
        // query whether or not the key has already been used
55
        if (isset($this->attributes[$key])) {
56
            throw new \Exception(sprintf('Try to override data with key %s', $key));
57
        }
58
59
        // set the attribute in the registry
60
        $this->attributes[$key] = $value;
61
    }
62
63
    /**
64
     * Return's the attribute with the passed key from the registry.
65
     *
66
     * @param mixed $key The key to return the attribute for
67
     *
68
     * @return mixed The requested attribute
69
     */
70
    public function getAttribute($key)
71
    {
72
        if (isset($this->attributes[$key])) {
73
            return $this->attributes[$key];
74
        }
75
    }
76
77
    /**
78
     * Query whether or not an attribute with the passed key has already been registered.
79
     *
80
     * @param mixed $key The key to query for
81
     *
82
     * @return boolean TRUE if the attribute has already been registered, else FALSE
83
     */
84
    public function hasAttribute($key)
85
    {
86
        return isset($this->attributes[$key]);
87
    }
88
89
    /**
90
     * Remove the attribute with the passed key from the registry.
91
     *
92
     * @param mixed $key The key of the attribute to return
93
     *
94
     * @return void
95
     */
96
    public function removeAttribute($key)
97
    {
98
        if (isset($this->attributes[$key])) {
99
            unset($this->attributes[$key]);
100
        }
101
    }
102
103
    /**
104
     * Raises the value for the attribute with the passed key by one.
105
     *
106
     * @param mixed $key         The key of the attribute to raise the value for
107
     * @param mixed $counterName The name of the counter to raise
108
     *
109
     * @return integer The counter's new value
110
     */
111
    public function raiseCounter($key, $counterName)
112
    {
113
114
        // raise/initialize the value
115
        if (isset($this->attributes[$key][$counterName])) {
116
            $this->attributes[$key][$counterName]++;
117
        } else {
118
            $this->attributes[$key][$counterName] = 1;
119
        }
120
121
        // return the new value
122
        return $this->attributes[$key][$counterName];
123
    }
124
125
    /**
126
     * This method merges the passed attributes with an array that
127
     * has already been added under the passed key.
128
     *
129
     * If no value will be found under the passed key, the attributes
130
     * will simply be registered.
131
     *
132
     * @param mixed $key        The key of the attributes that has to be merged with the passed ones
133
     * @param array $attributes The attributes that has to be merged with the exising ones
134
     *
135
     * @return void
136
     * @throws \Exception Is thrown, if the already registered value is no array
137
     * @link http://php.net/array_replace_recursive
138
     */
139
    public function mergeAttributesRecursive($key, array $attributes)
140
    {
141
142
        // if the key not exists, simply add the new attributes
143
        if (!isset($this->attributes[$key])) {
144
            $this->attributes[$key] = $attributes;
145
            return;
146
        }
147
148
        // if the key exists and the value is an array, merge it with the passed array
149
        if (isset($this->attributes[$key]) && is_array($this->attributes[$key])) {
150
            $this->attributes[$key] = array_replace_recursive($this->attributes[$key], $attributes);
151
            return;
152
        }
153
154
        // throw an exception if the key exists, but the found value is not of type array
155
        throw new \Exception(sprintf('Can\'t merge attributes, because value for key %s already exists, but is not of type array', $key));
156
    }
157
}
158