Passed
Branch feature/code-quality (2bf2a5)
by Filipe
13:16
created

Alias::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of slick/di package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Di\Definition;
11
12
use Psr\Container\ContainerInterface;
13
use Slick\Di\DefinitionInterface;
14
use Slick\Di\Exception\ContainerNotSetException;
15
use Slick\Di\Exception\NotFoundException;
16
17
/**
18
 * Alias Definition
19
 *
20
 * @package Slick\Di\Definition
21
 * @author  Filipe Silva <[email protected]>
22
 */
23
class Alias extends AbstractDefinition implements DefinitionInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $alias;
29
30
    /**
31
     * Alias needs a name and the definition name to look for in the container
32
     *
33
     * @param string $alias
34
     */
35
    public function __construct($alias)
36
    {
37
        $alias = str_replace('@', '', $alias);
38
39
        $this->alias = $alias;
40
    }
41
42
    /**
43
     * Resolves the definition into a scalar or object
44
     *
45
     * @return mixed
46
     *
47
     * @throws NotFoundException  No entry was found for alias
48
     *                            in current container.
49
     * @throws ContainerNotSetException If no container is set before
50
     *                                  calling resolve().
51
     */
52
    public function resolve()
53
    {
54
        if (!$this->container instanceof ContainerInterface) {
0 ignored issues
show
introduced by
$this->container is always a sub-type of Psr\Container\ContainerInterface.
Loading history...
55
            throw new ContainerNotSetException(
56
                "No container was set for definition. ".
57
                "It is not possible to look for alias '{$this->alias}'"
58
            );
59
        }
60
61
        return $this->container->get($this->alias);
62
    }
63
}
64