Delegator::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Di
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Di;
16
17
use Phossa2\Di\Message\Message;
18
use Phossa2\Di\Traits\ArrayAccessTrait;
19
use Phossa2\Shared\Base\ObjectAbstract;
20
use Interop\Container\ContainerInterface;
21
use Phossa2\Di\Exception\RuntimeException;
22
use Phossa2\Di\Exception\NotFoundException;
23
use Phossa2\Di\Interfaces\DelegatorInterface;
24
use Phossa2\Config\Interfaces\WritableInterface;
25
use Phossa2\Config\Traits\DelegatorWritableTrait;
26
27
/**
28
 * Delegator
29
 *
30
 * A writable and array accessable container delegator
31
 *
32
 * @package Phossa2\Di
33
 * @author  Hong Zhang <[email protected]>
34
 * @see     ObjectAbstract
35
 * @see     DelegatorInterface
36
 * @see     WritableInterface
37
 * @see     \ArrayAccess
38
 * @version 2.0.0
39
 * @since   2.0.0 added
40
 */
41
class Delegator extends ObjectAbstract implements DelegatorInterface, \ArrayAccess, WritableInterface
42
{
43
    use ArrayAccessTrait, DelegatorWritableTrait;
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public function get($id)
49
    {
50
        if ($this->hasInLookup($id)) {
51
            return $this->getFromLookup($id);
52
        } else {
53
            throw new NotFoundException(
54
                Message::get(Message::DI_SERVICE_NOTFOUND, $id),
55
                Message::DI_SERVICE_NOTFOUND
56
            );
57
        }
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function has($id)
64
    {
65
        return $this->hasInLookup($id);
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function set(/*# string */ $id, $value)/*# : bool */
72
    {
73
        if ($this->isWritable()) {
74
            return $this->writable->set($id, $value);
75
        } else {
76
            throw new RuntimeException(
77
                Message::get(Message::DI_CONTAINER_READONLY, $id),
78
                Message::DI_CONTAINER_READONLY
79
            );
80
        }
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function addContainer(ContainerInterface $container)
87
    {
88
        return $this->addRegistry($container);
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    protected function hasInRegistry(
95
        $registry,
96
        /*# string */ $key
97
    )/*# : bool */ {
98
        /* @var $registry ContainerInterface */
99
        return $registry->has($key);
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    protected function getFromRegistry(
106
        $registry,
107
        /*# string */ $key
108
    ) {
109
        /* @var $registry ContainerInterface */
110
        return $registry->get($key);
111
    }
112
}
113