Completed
Push — 4.x ( 8fa263...613328 )
by Phil
03:02
created

ContainerAwareTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 31
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getContainer() 0 8 2
A setContainer() 0 14 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Container;
6
7
use BadMethodCallException;
8
use League\Container\Exception\ContainerException;
9
10
trait ContainerAwareTrait
11
{
12
    /**
13
     * @var ?DefinitionContainerInterface
14
     */
15
    protected $container;
16
17 114
    public function setContainer(DefinitionContainerInterface $container): ContainerAwareInterface
18
    {
19 114
        $this->container = $container;
20
21 114
        if ($this instanceof ContainerAwareInterface) {
22 114
            return $this;
23
        }
24
25 3
        throw new BadMethodCallException(sprintf(
26 3
            'Attempt to use (%s) while not implementing (%s)',
27 3
            ContainerAwareTrait::class,
28 3
            ContainerAwareInterface::class
29
        ));
30
    }
31
32 129
    public function getContainer(): DefinitionContainerInterface
33
    {
34 129
        if ($this->container instanceof DefinitionContainerInterface) {
35 84
            return $this->container;
36
        }
37
38 51
        throw new ContainerException('No container implementation has been set.');
39
    }
40
}
41