Adapter   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 70.49%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 7
dl 0
loc 179
ccs 43
cts 61
cp 0.7049
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A factory() 0 19 1
A __construct() 0 4 1
B run() 0 25 3
A attachJobRunner() 0 8 1
A loadProjectsConfig() 0 8 1
A getProjectsConfig() 0 8 2
A getJobRunner() 0 8 2
A getHook() 0 10 2
A getInput() 0 12 2
A setHook() 0 4 1
A setInput() 0 4 1
A log() 0 6 2
1
<?php
2
3
namespace G2R;
4
5
use G2R\Config\ProjectConfig;
6
use G2R\Config\RundeckConfig;
7
use G2R\Exception\Exception;
8
use G2R\Gitlab\GitlabHook;
9
use G2R\Gitlab\HookResolver;
10
use G2R\Rundeck\JobRunner;
11
use Psr\Log\LoggerInterface;
12
use Psr\Log\LoggerTrait;
13
14
class Adapter
15
{
16
    use LoggerTrait;
17
18
    protected $logger;
19
20
    protected $jobRunner;
21
22
    protected $projectsConfig;
23
24
    protected $hook;
25
26
    protected $input;
27
28
    public static function factory(
29
        $rundeckConfig,
30
        $projectConfig,
31
        $logger = null
32
    ) {
33
        $adapter = new self($logger);
34
35
        // attach jobRunner
36
        $adapter->attachJobRunner(
37
            new JobRunner(new RundeckConfig($rundeckConfig))
38
        );
39
40
        // load the projects config
41
        $adapter->loadProjectsConfig(
42
            new ProjectConfig($projectConfig)
43
        );
44
45
        return $adapter;
46
    }
47
48
    /**
49
     * Eventually attach a Logger.
50
     *
51
     * @param LoggerInterface|null $logger
52
     */
53 12
    public function __construct(LoggerInterface $logger = null)
54
    {
55 12
        $this->logger = $logger;
56 12
    }
57
58
    /**
59
     * Run the adapter.
60
     *
61
     * @throws Exception
62
     */
63 10
    public function run()
64
    {
65 10
        $project = $this->getProjectsConfig()->getProject(
66 8
            $this->getHook()->getProjectName(),
67 8
            $this->getHook()->getRef()
68
        );
69
70
        // before we run the job, we want to make sure the build passed or
71
        // the runOnFail is true
72
73 8
        if ($this->getHook()->getBuildStatus() != 'success' &&
74 6
            !$project['runOnFail']
75
        ) {
76 2
            $this->warning(
77 2
                "The project config for {$this->getHook()->getProjectName()} ".
78 2
                "doesn't enable to run the job when the build failed"
79
            );
80
81 2
            return 0;
82
        }
83
84 6
        $this->info("Running the job {$project['jobId']}");
85
86 6
        return $this->getJobRunner()->run($project['jobId']);
87
    }
88
89
    /**
90
     * Load the rundeck job runner.
91
     *
92
     * @param JobRunner $jobRunner
93
     */
94 8
    public function attachJobRunner(JobRunner $jobRunner)
95
    {
96 8
        $this->info(
97 8
            "Job runner uses '{$jobRunner->getConfig()->getFilename()}' config"
98
        );
99
100 8
        $this->jobRunner = $jobRunner;
101 8
    }
102
103
    /**
104
     * Load the projects config.
105
     *
106
     * @param ProjectConfig $projectsConfig
107
     */
108 8
    public function loadProjectsConfig(ProjectConfig $projectsConfig)
109
    {
110 8
        $this->info(
111 8
            "Loading projects config file '{$projectsConfig->getFilename()}'"
112
        );
113
114 8
        $this->projectsConfig = $projectsConfig;
115 8
    }
116
117 10
    private function getProjectsConfig()
118
    {
119 10
        if (!isset($this->projectsConfig)) {
120 2
            throw new Exception('The projects config is missing');
121
        }
122
123 8
        return $this->projectsConfig;
124
    }
125
126 6
    private function getJobRunner()
127
    {
128 6
        if (!isset($this->jobRunner)) {
129 2
            throw new Exception('The Job runner is missing');
130
        }
131
132 4
        return $this->jobRunner;
133
    }
134
135
    /**
136
     * Get the hook.
137
     *
138
     * @return HookResolver
139
     */
140 8
    private function getHook()
141
    {
142 8
        if (!isset($this->hook)) {
143
            $this->hook = HookResolver::load($this->getInput());
144
145
            $this->debug('Hook handler type: '.get_class($this->hook));
146
        }
147
148 8
        return $this->hook;
149
    }
150
151
    /**
152
     * Get the input (php://input by default).
153
     *
154
     * @return string
155
     */
156
    private function getInput()
157
    {
158
        if (!isset($this->input)) {
159
            $this->debug('no input specified. Default php://input');
160
161
            $this->input = file_get_contents('php://input');
162
163
            $this->debug('input content: '.$this->input);
164
        }
165
166
        return $this->input;
167
    }
168
169 10
    public function setHook(GitlabHook $hook)
170
    {
171 10
        $this->hook = $hook;
172 10
    }
173
174
    public function setInput($input)
175
    {
176
        $this->input = $input;
177
    }
178
179
    /**
180
     * Call the logger.
181
     *
182
     * @param       $level
183
     * @param       $message
184
     * @param array $context
185
     */
186 12
    public function log($level, $message, array $context = [])
187
    {
188 12
        if ($this->logger) {
189 12
            $this->logger->log($level, $message, $context);
190
        }
191 12
    }
192
}
193