Reference::setName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2017 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Container;
9
10
/**
11
 * A reference to another entry in a {@link Container}.
12
 */
13
class Reference implements ReferenceInterface {
14
    /**
15
     * @var string|array
16
     */
17
    private $name;
18
19
    /**
20
     * @var array
21
     */
22
    private $args;
23
24
    /**
25
     * Construct a new instance of the {@link Reference} class.
26
     *
27
     * @param string|array $name The name of the reference.
28
     * @param array $args Constructor arguments for the reference.
29
     */
30 18
    public function __construct($name, array $args = []) {
31 18
        $this->setName($name);
32 18
        $this->setArgs($args);
33 18
    }
34
35
    /**
36
     * Get the name of the reference.
37
     *
38
     * @return string|array Returns the name of the reference.
39
     */
40 8
    public function getName() {
41 8
        return $this->name;
42
    }
43
44
    /**
45
     * Set the name of the reference.
46
     *
47
     * @param string|array $name The name of the reference.
48
     */
49 18
    public function setName($name) {
50 18
        $this->name = $name;
51 18
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 15
    public function resolve(Container $container, $_ = null) {
57 15
        if (empty($this->name)) {
58 1
            return null;
59 14
        } elseif (is_string($this->name)) {
60 11
            return $container->getArgs($this->name, $this->args);
61
        } else {
62 3
            $result = $container;
63 3
            foreach ($this->name as $name) {
64 3
                $result = $result->get($name);
65
            }
66 3
            return $result;
67
        }
68
    }
69
70
    /**
71
     * Get constructor arguments for the the reference.
72
     *
73
     * @return array Returns the arguments.
74
     */
75
    public function getArgs() {
76
        return $this->args;
77
    }
78
79
    /**
80
     * Set constructor arguments for the the reference.
81
     *
82
     * @param array $args An array of arguments.
83
     * @return $this
84
     */
85 18
    public function setArgs($args) {
86 18
        $this->args = $args;
87 18
        return $this;
88
    }
89
}
90