Completed
Push — master ( ed42ad...75c416 )
by Rémi
05:47 queued 04:05
created

JobRunner   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 52.63%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 69
ccs 10
cts 19
cp 0.5263
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getConfig() 0 4 1
A run() 0 20 3
A getApiUrl() 0 8 3
1
<?php
2
3
namespace G2R\Rundeck;
4
5
use G2R\Config\RundeckConfig;
6
use G2R\Exception\Exception;
7
8
class JobRunner
9
{
10
    protected $config;
11
12 4
    public function __construct(RundeckConfig $config)
13
    {
14 4
        $this->config = $config;
15 4
    }
16
17
    /**
18
     * return the Rundeck configuration.
19
     *
20
     * @return RundeckConfig
21
     */
22 2
    public function getConfig()
23
    {
24 2
        return $this->config;
25
    }
26
27
    /**
28
     * Send the request to rundesk.
29
     *
30
     * @param       $jobId
31
     * @param array $parameters
32
     */
33
    public function run($jobId, array $parameters = [])
34
    {
35
        $method = (empty($parameters)) ? 'GET' : 'POST';
36
37
        $options = [
38
            'http' => [
39
                'header' => "X-Rundeck-Auth-Token: {$this->config['token']}\r\n",
40
                'method' => $method,
41
            ],
42
        ];
43
44
        if ($method == 'POST') {
45
            $options['http']['header'] .= "Content-type: application/x-www-form-urlencoded\r\n";
46
            $options['http']['content'] = http_build_query($parameters);
47
        }
48
49
        $context = stream_context_create($options);
50
51
        return file_get_contents($this->getApiUrl($jobId), false, $context);
52
    }
53
54
    /**
55
     * Return the formatted url for a GET or POST rundeck request to run a job.
56
     *
57
     * http://rundeck.org/docs/api/index.html#running-a-job
58
     *      GET /api/1/job/[ID]/run
59
     *      POST /api/12/job/[ID]/executions
60
     *
61
     * @param        $jobId
62
     * @param string $method
63
     *
64
     * @throws Exception
65
     *
66
     * @return string
67
     */
68 2
    public function getApiUrl($jobId, $method = 'GET')
69
    {
70
        return
71 2
            'http'.(($this->config['ssl']) ? 's' : '').'://'.
72 2
            $this->config['host'].':'.$this->config['port'].'/api/'.
73 2
            $this->config['api_version'].'/job/'.$jobId.'/'.
74 2
            ((strcasecmp($method, 'get') == 0) ? 'run' : 'executions');
75
    }
76
}
77