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

ContainerAwareTrait::setContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 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