ParameterNotFoundException::setSource()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the Borobudur-DependencyInjection package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\DependencyInjection\Exception;
12
13
/**
14
 * @author      Iqbal Maulana <[email protected]>
15
 * @created     8/7/15
16
 */
17
class ParameterNotFoundException extends InvalidArgumentException
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $name;
23
24
    /**
25
     * @var string
26
     */
27
    protected $sourceName;
28
29
    /**
30
     * @var array
31
     */
32
    protected $alternatives = array();
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param string $name
38
     * @param array  $alternatives
39
     */
40
    public function __construct($name, array $alternatives = array())
41
    {
42
        $this->name = $name;
43
        $this->alternatives = $alternatives;
44
45
        parent::__construct('');
46
47
        $this->update();
48
    }
49
50
    /**
51
     * Set source name.
52
     *
53
     * @param string $sourceName
54
     */
55
    public function setSource($sourceName)
56
    {
57
        $this->sourceName = $sourceName;
58
59
        $this->update();
60
    }
61
62
    /**
63
     * Update exception message
64
     */
65
    protected function update()
66
    {
67
        $this->message = $this->getMessageDefinition();
68
69
        if (!empty($this->alternatives)) {
70
            if (1 === count($this->alternatives)) {
71
                $format = 'Did you mean this: "%s"?';
72
            } else {
73
                $format = 'Did you mean on of these: "%s"?';
74
            }
75
76
            $this->message .= sprintf(' ' . $format, implode(', ', $this->alternatives));
77
        }
78
    }
79
80
    /**
81
     * Get message definition.
82
     *
83
     * @return string
84
     */
85
    protected function getMessageDefinition()
86
    {
87
        if (null !== $this->sourceName) {
88
            return sprintf('Parameter "%s" depend on undefined parameter "%s".', $this->sourceName, $this->name);
89
        }
90
91
        return sprintf('Parameter "%s" is undefined.', $this->name);
92
    }
93
}
94