Context   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 82.14%

Importance

Changes 0
Metric Value
dl 0
loc 118
ccs 23
cts 28
cp 0.8214
rs 10
c 0
b 0
f 0
wmc 12
lcom 2
cbo 2

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A push() 0 4 1
A has() 0 4 1
A get() 0 7 2
A pop() 0 4 1
A required() 0 6 2
A getConfig() 0 4 1
A getInput() 0 4 1
A getOutput() 0 4 1
A getHost() 0 4 1
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Task;
9
10
use Deployer\Configuration\Configuration;
11
use Deployer\Exception\Exception;
12
use Deployer\Host\Host;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
class Context
17
{
18
    /**
19
     * @var Host
20
     */
21
    private $host;
22
23
    /**
24
     * @var InputInterface
25
     */
26
    private $input;
27
28
    /**
29
     * @var OutputInterface
30
     */
31
    private $output;
32
33
    /**
34
     * @var Context[]
35
     */
36
    private static $contexts = [];
37
38
    /**
39
     * @param Host $host
40
     * @param InputInterface $input
41
     * @param OutputInterface $output
42
     */
43 24
    public function __construct($host, InputInterface $input = null, OutputInterface $output = null)
44
    {
45 24
        $this->host = $host;
46 24
        $this->input = $input;
47 24
        $this->output = $output;
48 24
    }
49
50
    /**
51
     * @param Context $context
52
     */
53 27
    public static function push(Context $context)
54
    {
55 27
        self::$contexts[] = $context;
56 27
    }
57
58
    /**
59
     * @return bool
60
     */
61 16
    public static function has()
62
    {
63 16
        return !empty(self::$contexts);
64
    }
65
66
    /**
67
     * @return Context|false
68
     * @throws Exception
69
     */
70 18
    public static function get()
71
    {
72 18
        if (empty(self::$contexts)) {
73
            throw new Exception('Context was required, but there\'s nothing there.');
74
        }
75 18
        return end(self::$contexts);
76
    }
77
78
    /**
79
     * @return Context
80
     */
81 28
    public static function pop()
82
    {
83 28
        return array_pop(self::$contexts);
84
    }
85
86
    /**
87
     * Throws a Exception when not called within a task-context and therefore no Context is available.
88
     *
89
     * This method provides a useful error to the end-user to make him/her aware
90
     * to use a function in the required task-context.
91
     *
92
     * @param string $callerName
93
     * @throws Exception
94
     */
95
    public static function required($callerName)
96
    {
97
        if (!self::get()) {
98
            throw new Exception("'$callerName' can only be used within a task.");
99
        }
100
    }
101
102
    /**
103
     * @return Configuration
104
     */
105 15
    public function getConfig()
106
    {
107 15
        return $this->host->getConfig();
108
    }
109
110
    /**
111
     * @return InputInterface
112
     */
113 10
    public function getInput()
114
    {
115 10
        return $this->input;
116
    }
117
118
    /**
119
     * @return OutputInterface
120
     */
121 16
    public function getOutput()
122
    {
123 16
        return $this->output;
124
    }
125
126
    /**
127
     * @return Host
128
     */
129 13
    public function getHost()
130
    {
131 13
        return $this->host;
132
    }
133
}
134