Completed
Push — develop ( f11aab...a4697b )
by Alejandro
09:31
created

ServiceManagerMock::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace AcMailerTest\ServiceManager;
3
4
use Interop\Container\ContainerInterface;
5
use Zend\ServiceManager\Exception;
6
7
/**
8
 * Class ServiceManagerMock
9
 * @author Alejandro Celaya Alastrué
10
 * @link http://www.alejandrocelaya.com
11
 */
12
class ServiceManagerMock implements ContainerInterface
13
{
14
    /**
15
     * @var array()
16
     */
17
    private $services;
18
19
    public function __construct(array $services = [])
20
    {
21
        $this->services = $services;
22
    }
23
24
    /**
25
     * Retrieve a registered instance
26
     *
27
     * @param  string $name
28
     * @throws Exception\ServiceNotFoundException
29
     * @return object|array
30
     */
31
    public function get($name)
32
    {
33
        if (!$this->has($name)) {
34
            throw new Exception\ServiceNotFoundException(sprintf(
35
                "Service with name %s not found",
36
                $name
37
            ));
38
        }
39
40
        return $this->services[$name];
41
    }
42
43
    /**
44
     * Check for a registered instance
45
     *
46
     * @param  string|array $name
47
     * @return bool
48
     */
49
    public function has($name)
50
    {
51
        return array_key_exists($name, $this->services);
52
    }
53
54
    /**
55
     * Sets the service with defined key
56
     * @param $key
57
     * @param $service
58
     * @return $this
59
     */
60
    public function set($key, $service)
61
    {
62
        $this->services[$key] = $service;
63
        return $this;
64
    }
65
}
66