ContainerAwareTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 12
c 2
b 0
f 0
dl 0
loc 29
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getContainer() 0 7 2
A setContainer() 0 12 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 138
    public function setContainer(DefinitionContainerInterface $container): ContainerAwareInterface
18
    {
19 138
        $this->container = $container;
20
21 138
        if ($this instanceof ContainerAwareInterface) {
22 138
            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 3
        ));
30
    }
31
32 153
    public function getContainer(): DefinitionContainerInterface
33
    {
34 153
        if ($this->container instanceof DefinitionContainerInterface) {
35 108
            return $this->container;
36
        }
37
38 51
        throw new ContainerException('No container implementation has been set.');
39
    }
40
}
41