ContainerAwareTrait::setContainer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the uSilex framework.
5
 *
6
 * (c) Enrico Fagnoni <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace uSilex;
13
14
use Psr\Container\ContainerInterface;
15
use Pimple\Container as PimpleContainer;
16
17
/**
18
 * Psr11 trait. Realize a Container aware interfce from a pimple container
19
 *
20
 * @author Enrico Fagnoni <[email protected]>
21
 */
22
trait ContainerAwareTrait
23
{
24
    private $container = null;
25
    
26 2
    public function __construct(ContainerInterface $container = null)
27
    {
28 2
        $this->container = $container;
29 2
    }
30
    
31 1
    public function getContainer() : ContainerInterface
32
    {
33 1
        return $this->container;
34
    }
35
    
36
    
37 1
    public function setContainer(ContainerInterface $container) : self
38
    {
39 1
        $this->container = $container;
40
        
41 1
        return $this;
42
    }
43
    
44
    
45
    /**
46
     * A shortcut to get a value with a default
47
     */
48 2
    public function containerGet(String $id, $value = null)
49
    {
50 2
        return ($this->container && $this->container->has($id))
51 1
            ? $this->container->get($id)
52 2
            : $value;
53
    }
54
}
55