Completed
Pull Request — master (#37)
by Alberto
01:52
created

ProxyContainer::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Proxy;
5
6
use Moka\Exception\InvalidIdentifierException;
7
use Psr\Container\ContainerInterface;
8
9
/**
10
 * Class ProxyContainer
11
 * @package Moka\Proxy
12
 */
13
final class ProxyContainer implements ContainerInterface
14
{
15
    /**
16
     * @var array
17
     */
18
    private $mocks;
19
20
    /**
21
     * ProxyContainer constructor.
22
     */
23 12
    public function __construct()
24
    {
25 12
        $this->mocks = [];
26
    }
27
28
    /**
29
     * @param string $id
30
     * @return ProxyInterface
31
     *
32
     * @throws InvalidIdentifierException
33
     */
34 6
    public function get($id): ProxyInterface
35
    {
36 6
        if (!$this->has($id)) {
37 1
            throw new InvalidIdentifierException(
38 1
                sprintf(
39 1
                    'Cannot find mock object with identifier "%s"',
40 1
                    $id
41
                )
42
            );
43
        }
44
45 5
        return $this->mocks[$id];
46
    }
47
48
    /**
49
     * @param string $id
50
     * @return bool
51
     */
52 11
    public function has($id): bool
53
    {
54 11
        return isset($this->mocks[$id]);
55
    }
56
57
    /**
58
     * @param string $id
59
     * @param ProxyInterface $mock
60
     */
61 8
    public function set(string $id, ProxyInterface $mock)
62
    {
63 8
        $this->mocks[$id] = $mock;
64
    }
65
}
66