Passed
Pull Request — master (#56)
by Evandro
07:37
created

Assert::keyIsClass()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php namespace Chekote\NounStore;
2
3
use InvalidArgumentException;
4
use OutOfBoundsException;
5
use RuntimeException;
6
7
/**
8
 * Makes assertions regarding store data.
9
 */
10
class Assert
11
{
12
    /** @var Store */
13
    protected $store;
14
15
    /** @var Key */
16
    protected $keyService;
17
18
    /**
19
     * Assert constructor.
20
     *
21
     * @param Store    $store      the store to assert against.
22
     * @param Key|null $keyService the keyService to use for key parsing/building. Will use the default Key service
23
     *                             if not specified.
24
     *
25
     * @codeCoverageIgnore
26
     */
27
    public function __construct(Store $store, Key $keyService = null)
28
    {
29
        $this->store = $store;
30
        $this->keyService = $keyService ?: Key::getInstance();
31
    }
32
33
    /**
34
     * Asserts that a value has been stored for the specified key.
35
     *
36
     * @see    Key::build()
37
     * @see    Key::parseNoun()
38
     * @param  string                   $key The key to check. Supports nth notation.
39
     * @throws OutOfBoundsException     if a value has not been stored for the specified key.
40
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
41
     *                                      that does not match the index.
42
     * @return mixed                    The value.
43 4
     */
44
    public function keyExists($key)
45 4
    {
46
        if (!$this->store->keyExists($key)) {
47 3
            throw new OutOfBoundsException("Entry '$key' was not found in the store.");
48 1
        }
49 1
50
        return $this->store->get($key);
51
    }
52
53 2
    /**
54
     * Asserts that the key's value contains the specified string.
55
     *
56
     * @see    Key::build()
57
     * @see    Key::parseNoun()
58
     * @param  string                   $key   The key to check. Supports nth notation.
59
     * @param  string                   $value The value expected to be contained within the key's value.
60
     * @throws OutOfBoundsException     If a value has not been stored for the specified key.
61
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
62
     *                                        that does not match the index.
63
     */
64
    public function keyValueContains($key, $value)
65
    {
66
        $this->keyExists($key);
67 5
68
        if (!$this->store->keyValueContains($key, $value)) {
69 5
            throw new RuntimeException("Entry '$key' does not contain '$value'");
70
        }
71 4
    }
72
73 3
    /**
74 1
     * Asserts that the key's value matches the specified value.
75 1
     *
76
     * @see    Key::build()
77
     * @see    Key::parseNoun()
78 2
     * @param  string                   $key   The key to check. Supports nth notation.
79
     * @param  mixed                    $value The expected value.
80
     * @throws OutOfBoundsException     If a value has not been stored for the specified key.
81
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
82
     *                                        that does not match the index.
83
     */
84
    public function keyValueIs($key, $value)
85
    {
86
        if ($this->keyExists($key) != $value) {
87
            throw new RuntimeException("Entry '$key' does not match '" . print_r($value, true) . "'");
0 ignored issues
show
Bug introduced by
Are you sure print_r($value, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
            throw new RuntimeException("Entry '$key' does not match '" . /** @scrutinizer ignore-type */ print_r($value, true) . "'");
Loading history...
88
        }
89
    }
90
91 5
    /**
92
     * Asserts that the key's value matches the specified class instance.
93 5
     *
94
     * @see    Key::build()
95 4
     * @see    Key::parseNoun()
96
     * @param  string $key          The key to check. Supports nth notation.
97 3
     * @param  string $class        The expected class instance.
98 1
     * @throws OutOfBoundsException If a value has not been stored for the specified key.
99 1
     */
100
    public function keyIsClass($key, $class)
101
    {
102 2
        if ($this->keyExists($key) && !$this->store->keyIsClass($key, $class)) {
103
            throw new RuntimeException("Entry '$key' does not match instance of '$class'");
104
        }
105
    }
106
}
107