Completed
Branch master (c930c0)
by Alejandro
10:35 queued 05:57
created

Container::initServiceManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 1
cts 1
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
namespace Acelaya\SlimContainerSm;
3
4
use Acelaya\SlimContainerSm\Exception\BadMethodCallException;
5
use Acelaya\SlimContainerSm\Factory\CallbackWrapper;
6
use Slim\Helper\Set;
7
use Zend\ServiceManager\ServiceManager;
8
9
/**
10
 * Class Container
11
 * @author Alejandro Celaya Alastrué
12
 * @link http://www.alejandrocelaya.com
13
 */
14
class Container extends Set implements SlimContainerConsumibleInterface
15
{
16
    /**
17
     * @var ServiceManager
18
     */
19
    protected $sm;
20
21
    public function __construct(ServiceManager $sm = null)
22
    {
23 14
        parent::__construct();
24
        $this->initServiceManager($sm);
25 14
    }
26 14
27 14
    protected function initServiceManager(ServiceManager $sm = null)
28
    {
29
        $this->sm = $sm ?: new ServiceManager();
30
        $this->sm->setAllowOverride(true);
31
    }
32
33
    /**
34 6
     * Set data key to value
35
     * @param string $key   The data key
36 6
     * @param mixed  $value The data value
37 1
     */
38 1
    public function set($key, $value)
39 6
    {
40
        if (is_callable($value)) {
41 6
            $this->registerFactory($key, $value, false);
42
        } else {
43
            $this->sm->setService($key, $value);
44
        }
45
    }
46
47
    /**
48
     * Get data value with key
49 3
     * @param  string $key     The data key
50
     * @param  mixed  $default The value to return if data key does not exist
51 3
     * @return mixed           The data value, or the default value
52
     */
53
    public function get($key, $default = null)
54
    {
55
        return $this->has($key) ? $this->sm->get($key) : $default;
56
    }
57
58 1
    /**
59
     * Fetch set data
60 1
     * @return array This set's key-value data array
61
     */
62
    public function all()
63
    {
64
        throw new BadMethodCallException('It is not possible to fetch all services registered in a ServiceManager');
65
    }
66
67 4
    /**
68
     * Fetch set data keys
69 4
     * @return array This set's key-value data array keys
70
     */
71
    public function keys()
72
    {
73
        throw new BadMethodCallException('It is not possible to get service names registered in a ServiceManager');
74
    }
75
76
    /**
77 7
     * Does this set contain a key?
78
     * @param  string  $key The data key
79 7
     * @return boolean
80
     */
81
    public function has($key)
82
    {
83
        return $this->sm->has($key);
84
    }
85
86 1
    /**
87
     * Remove value with key from this set
88 1
     * @param string $key The data key
89 1
     */
90
    public function remove($key)
91
    {
92
        $this->sm->setService($key, null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
    }
94 1
95
    /**
96 1
     * Clear all values
97 1
     */
98 1
    public function clear()
99 1
    {
100 1
        $this->initServiceManager();
101
    }
102
103
    /**
104
     * Countable
105 1
     */
106
    public function count()
107 1
    {
108
        throw new BadMethodCallException('It is not possible to count the number of services in a ServiceManager');
109
    }
110
111
    /**
112
     * IteratorAggregate
113 1
     */
114
    public function getIterator()
115 1
    {
116
        throw new BadMethodCallException('It is not possible to generate an iterator from a ServiceManager');
117
    }
118
119
    /**
120
     * Ensure a value or object will remain globally unique
121
     * @param string $key The value or object name
122
     * @param $value
123
     * @return mixed
124 2
     */
125
    public function singleton($key, $value)
126 2
    {
127 1
        if (is_callable($value)) {
128 1
            $this->registerFactory($key, $value);
129 1
        } else {
130
            $this->sm->setService($key, $value);
131 2
        }
132
    }
133
134
    /**
135
     * Makes this to consume the services defined in provided container
136
     *
137 14
     * @param Set $container
138
     * @return mixed
139 14
     */
140 14
    public function consumeSlimContainer(Set $container)
141 14
    {
142
        foreach ($container as $key => $value) {
143
            if ($value instanceof \Closure) {
144
                // Try to determin if this belongs to a singleton or not
145
                $refFunc = new \ReflectionFunction($value);
146 1
                // Slim singletons have a static 'object' variable
147
                $shared = in_array('object', $refFunc->getStaticVariables());
148 1
                $this->registerFactory($key, $value, $shared);
149
            } elseif (is_callable($value)) {
150
                // Register as non-shared factories any other callable
151
                $this->registerFactory($key, $value, false);
152
            } else {
153
                $this->sm->setService($key, $value);
154
            }
155
        }
156
    }
157 1
158
    /**
159 1
     * Registers a factory wrapping a Slim factory into a ZF2 factory
160 1
     *
161
     * @param $key
162 1
     * @param callable $callable
163
     * @param bool $shared
164 1
     */
165 1
    protected function registerFactory($key, callable $callable, $shared = true)
166 1
    {
167
        $this->sm->setFactory($key, new CallbackWrapper($this, $callable));
168 1
        $this->sm->setShared($key, $shared);
169 1
    }
170
}
171