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

Assert::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php namespace Chekote\NounStore;
2
3
use InvalidArgumentException;
4
use OutOfBoundsException;
5
6
/**
7
 * Makes assertions regarding store data.
8
 */
9
class Assert
10
{
11
    /** @var Store */
12
    protected $store;
13
14
    /** @var Key */
15
    protected $keyService;
16
17
    /**
18
     * Assert constructor.
19
     *
20
     * @param Store $store      the store to assert against.
21
     * @param Key   $keyService the keyService to use for key parsing/building.
22
     *
23
     * @codeCoverageIgnore
24
     */
25
    public function __construct(Store $store, Key $keyService)
26
    {
27
        $this->store = $store;
28
        $this->keyService = $keyService;
29
    }
30
31
    /**
32
     * Asserts that a value has been stored for the specified key.
33
     *
34
     * @param  string                   $key   The key to check. @see self::get() for formatting options.
35
     * @param  int                      $index [optional] The index of the key entry to check. If not specified, the
36
     *                                         method will ensure that at least one item is stored for the key.
37
     * @throws OutOfBoundsException     if a value has not been stored for the specified key.
38
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
39
     *                                        that does not match the index.
40
     * @return mixed                    The value.
41
     */
42 21 View Code Duplication
    public function keyExists($key, $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...
43
    {
44 21
        list($key, $index) = $this->keyService->parse($key, $index);
45
46 20
        if (!$this->store->keyExists($key, $index)) {
47 5
            throw new OutOfBoundsException(
48 5
                "Entry '" . $this->keyService->build($key, $index) . "' was not found in the store."
49
            );
50
        }
51
52 15
        return $this->store->get($key, $index);
53
    }
54
}
55