Passed
Pull Request — master (#12)
by Donald
02:51 queued 01:11
created

Store::keyValueContains()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Chekote\NounStore;
2
3
use InvalidArgumentException;
4
use OutOfBoundsException;
5
use RuntimeException;
6
7
class Store
8
{
9
    /** @var array */
10
    protected $nouns;
11
12
    /** @var Key */
13
    protected $keyService;
14
15
    /** @var Assert */
16
    protected $assert;
17
18 47
    public function __construct(Key $keyService = null)
19
    {
20 47
        $this->keyService = $keyService ?: Key::getInstance();
21
22
        // @todo this is temporary. remove once all assert methods are moved to Assert class
23 47
        $this->assert = new Assert($this, $this->keyService);
24 47
    }
25
26
    /**
27
     * Asserts that the key's value matches the specified value.
28
     *
29
     * @param  string                   $key   The key to check. @see self::get() for formatting options.
30
     * @param  mixed                    $value The expected value.
31
     * @param  int                      $index [optional] The index of the key entry to retrieve. If not specified, the
32
     *                                         method will check the most recent value stored under the key.
33
     * @throws OutOfBoundsException     If a value has not been stored for the specified key.
34
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
35
     *                                        that does not match the index.
36
     */
37 10
    public function assertKeyValueIs($key, $value, $index = null)
38
    {
39 10
        list($key, $index) = $this->keyService->parse($key, $index);
40
41 9
        $this->assert->keyExists($key, $index);
42
43 7
        if ($this->get($key, $index) != $value) {
44 2
            throw new RuntimeException(
45 2
                "Entry '" . $this->keyService->build($key, $index) . "' does not match '" . print_r($value, true) . "'"
46
            );
47
        }
48 5
    }
49
50
    /**
51
     * Asserts that the key's value contains the specified string.
52
     *
53
     * @param  string                   $key   The key to check. @see self::get() for formatting options.
54
     * @param  string                   $value The value expected to be contained within the key's value.
55
     * @param  int                      $index [optional] The index of the key entry to retrieve. If not specified, the
56
     *                                         method will check the most recent value stored under the key.
57
     * @throws OutOfBoundsException     If a value has not been stored for the specified key.
58
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
59
     *                                        that does not match the index.
60
     */
61 10 View Code Duplication
    public function assertKeyValueContains($key, $value, $index = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63 10
        list($key, $index) = $this->keyService->parse($key, $index);
64
65 9
        $this->assert->keyExists($key, $index);
66
67 7
        if (!$this->keyValueContains($key, $value, $index)) {
68 2
            throw new RuntimeException(
69 2
                "Entry '" . $this->keyService->build($key, $index) . "' does not contain '$value'"
70
            );
71
        }
72 5
    }
73
74
    /**
75
     * Removes all entries from the store.
76
     *
77
     * @return void
78
     */
79 1
    public function reset()
80
    {
81 1
        $this->nouns = [];
82 1
    }
83
84
    /**
85
     * Retrieves a value for the specified key.
86
     *
87
     * Each key is actually a collection. If you do not specify which item in the collection you want,
88
     * the method will return the most recent entry. You can specify the entry you want by either
89
     * using the plain english 1st, 2nd, 3rd etc in the $key param, or by specifying 0, 1, 2 etc in
90
     * the $index param. For example:
91
     *
92
     * Retrieve the most recent entry "Thing" collection:
93
     *   retrieve("Thing")
94
     *
95
     * Retrieve the 1st entry in the "Thing" collection:
96
     *   retrieve("1st Thing")
97
     *   retrieve("Thing", 0)
98
     *
99
     * Retrieve the 3rd entry in the "Thing" collection:
100
     *   retrieve("3rd Thing")
101
     *   retrieve("Thing", 2)
102
     *
103
     * Please note: The nth value in the string key is indexed from 1. In that "1st" is the first item stored.
104
     * The index parameter is indexed from 0. In that 0 is the first item stored.
105
     *
106
     * Please Note: If you specify both an $index param and an nth in the $key, they must both reference the same index.
107
     * If they do not, the method will throw an InvalidArgumentException.
108
     *
109
     * retrieve("1st Thing", 1);
110
     *
111
     * @param  string                   $key   The key to retrieve the value for. Can be prefixed with an nth descriptor.
112
     * @param  int                      $index [optional] The index of the key entry to retrieve. If not specified, the
113
     *                                         method will return the most recent value stored under the key.
114
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
115
     *                                        that does not match the index.
116
     * @return mixed                    The value, or null if no value exists for the specified key/index combination.
117
     */
118 30
    public function get($key, $index = null)
119
    {
120 30
        list($key, $index) = $this->keyService->parse($key, $index);
121
122 29
        if (!$this->keyExists($key, $index)) {
123 5
            return;
124
        }
125
126 24
        return $index !== null ? $this->nouns[$key][$index] : end($this->nouns[$key]);
127
    }
128
129
    /**
130
     * Retrieves all values for the specified key.
131
     *
132
     * @param  string               $key The key to retrieve the values for.
133
     * @throws OutOfBoundsException if the specified $key does not exist in the store.
134
     * @return array                The values.
135
     */
136 2
    public function getAll($key)
137
    {
138 2
        if (!isset($this->nouns[$key])) {
139 1
            throw new OutOfBoundsException("'$key' does not exist in the store");
140
        }
141
142 1
        return $this->nouns[$key];
143
    }
144
145
    /**
146
     * Determines if a value has been stored for the specified key.
147
     *
148
     * @param  string                   $key   The key to check.
149
     * @param  int                      $index [optional] The index of the key entry to check. If not specified, the
150
     *                                         method will ensure that at least one item is stored for the key.
151
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
152
     *                                        that does not match the index.
153
     * @return bool                     True if the a value has been stored, false if not.
154
     */
155 38
    public function keyExists($key, $index = null)
156
    {
157 38
        list($key, $index) = $this->keyService->parse($key, $index);
158
159 37
        return $index !== null ? isset($this->nouns[$key][$index]) : isset($this->nouns[$key]);
160
    }
161
162
    /**
163
     * Asserts that the key's value contains the specified string.
164
     *
165
     * @param  string                   $key   The key to check. @see self::get() for formatting options.
166
     * @param  string                   $value The value expected to be contained within the key's value.
167
     * @param  int                      $index [optional] The index of the key entry to retrieve. If not specified, the
168
     *                                         method will check the most recent value stored under the key.
169
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
170
     *                                        that does not match the index.
171
     * @return bool                     True if the key's value contains the specified string, false if not.
172
     */
173 17
    public function keyValueContains($key, $value, $index = null)
174
    {
175 17
        list($key, $index) = $this->keyService->parse($key, $index);
176
177 16
        $actual = $this->get($key, $index);
178
179 16
        return is_string($actual) && strpos($actual, $value) !== false;
180
    }
181
182
    /**
183
     * Stores a value for the specified key.
184
     *
185
     * @param string $key   The key to store the value under.
186
     * @param mixed  $value The value to store.
187
     */
188 47
    public function set($key, $value)
189
    {
190 47
        $this->nouns[$key][] = $value;
191 47
    }
192
}
193