ServiceProviderPimpleBridge::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace TheCodingMachine\Pimple;
4
5
use Acclimate\Container\CompositeContainer;
6
use Acclimate\Container\ContainerAcclimator;
7
use Interop\Container\ContainerInterface;
8
use Interop\Container\ServiceProvider;
9
use Pimple\Container;
10
11
/**
12
 * A bridge that allows you to register container-interop/service-providers in Pimple 3.
13
 */
14
class ServiceProviderPimpleBridge
15
{
16
    /**
17
     * @var Container
18
     */
19
    private $pimple;
20
21
    /**
22
     * @var ContainerInterface
23
     */
24
    private $acclimatedPimple;
25
26
    /**
27
     * @param Container $pimple
28
     */
29
    public function __construct(Container $pimple)
30
    {
31
        $acclimator = new ContainerAcclimator();
32
        $this->pimple = $pimple;
33
        $this->acclimatedPimple = $acclimator->acclimate($pimple);
34
    }
35
36
    /**
37
     * Registers a service provider.
38
     *
39
     * @param ServiceProvider $provider the service provider to register
40
     * @param array $values An array of values that customizes the provider
41
     *
42
     * @return static
43
     */
44
    public function register(ServiceProvider $provider, array $values = array())
45
    {
46
        $entries = $provider->getServices();
47
        foreach ($entries as $key => $callable) {
48
            if (isset($this->pimple[$key])) {
49
                // Extend a previous entry
50
                $this->pimple[$key] = $this->pimple->extend($key, function ($previous, Container $c) use ($callable) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
                    $getPrevious = function () use ($previous) {
52
                        return $previous;
53
                    };
54
                    return call_user_func($callable, $this->acclimatedPimple, $getPrevious);
55
                });
56
            } else {
57
                $this->pimple[$key] = function (Container $c) use ($callable) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
                    return call_user_func($callable, $this->acclimatedPimple, null);
59
                };
60
            }
61
        }
62
        foreach ($values as $key => $value) {
63
            $this->pimple[$key] = $value;
64
        }
65
        return $this;
66
    }
67
}
68