Completed
Push — master ( 2883bd...d25167 )
by Lukáš
06:33 queued 03:16
created

Execution::bulkDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Lookyman\Rundeck\Api\Endpoints\System;
5
6
use GuzzleHttp\Promise\PromiseInterface;
7
use Lookyman\Rundeck\Api\Client;
8
use GuzzleHttp\Psr7\Request;
9
10
class Execution
11
{
12
	/**
13
	 * @var Client
14
	 */
15
	private $client;
16
17
	/**
18
	 * @param Client $client
19
	 */
20
	public function __construct(Client $client)
21
	{
22
		$this->client = $client;
23
	}
24
25
	/**
26
	 * @param string $id
27
	 * @return PromiseInterface
28
	 */
29
	public function info(string $id): PromiseInterface
30
	{
31
		return $this->client->getConfiguration()->getGuzzle()->sendAsync(
32
			new Request('GET', $this->client->getConfiguration()->getBaseUri() . sprintf('/execution/%s', urlencode($id)))
33
		);
34
	}
35
36
	/**
37
	 * @param string $id
38
	 * @return PromiseInterface
39
	 */
40
	public function delete(string $id): PromiseInterface
41
	{
42
		return $this->client->getConfiguration()->getGuzzle()->sendAsync(
43
			new Request('DELETE', $this->client->getConfiguration()->getBaseUri() . sprintf('/execution/%s', urlencode($id)))
44
		);
45
	}
46
47
	/**
48
	 * @param int[] $ids
49
	 * @return PromiseInterface
50
	 */
51
	public function bulkDelete(array $ids): PromiseInterface
52
	{
53
		return $this->client->getConfiguration()->getGuzzle()->sendAsync(
54
			new Request('POST', $this->client->getConfiguration()->getBaseUri() . '/executions/delete', [], $this->client->getConfiguration()->getFormat()->formatParams(['ids' => $ids]))
55
		);
56
	}
57
58
	/**
59
	 * @param string $id
60
	 * @return PromiseInterface
61
	 */
62
	public function state(string $id): PromiseInterface
63
	{
64
		return $this->client->getConfiguration()->getGuzzle()->sendAsync(
65
			new Request('GET', $this->client->getConfiguration()->getBaseUri() . sprintf('/execution/%s/state', urlencode($id)))
66
		);
67
	}
68
69
	/**
70
	 * @param string $id
71
	 * @param string|null $node
72
	 * @param string|null $step
73
	 * @param array $params
74
	 * @return PromiseInterface
75
	 */
76
	public function output(string $id, string $node = null, string $step = null, array $params = []): PromiseInterface
77
	{
78
		if ($node && $step) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $node of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $step of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
79
			$path = sprintf('/execution/%s/output/node/%s/step/%s', urlencode($id), urlencode($node), urlencode($step));
80
		} elseif ($node && !$step) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $node of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $step of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
81
			$path = sprintf('/execution/%s/output/node/%s', urlencode($id), urlencode($node));
82
		} elseif (!$node && $step) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $node of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $step of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
83
			$path = sprintf('/execution/%s/output/step/%s', urlencode($id), urlencode($step));
84
		} else {
85
			$path = sprintf('/execution/%s/output', urlencode($id));
86
		}
87
88
		return $this->client->getConfiguration()->getGuzzle()->sendAsync(
89
			new Request('GET', $this->client->getConfiguration()->getBaseUri() . $path, [], $this->client->getConfiguration()->getFormat()->formatParams($params))
90
		);
91
	}
92
93
	/**
94
	 * @param string $id
95
	 * @param bool $stateOnly
96
	 * @return PromiseInterface
97
	 */
98
	public function outputWithState(string $id, bool $stateOnly = false): PromiseInterface
99
	{
100
		return $this->client->getConfiguration()->getGuzzle()->sendAsync(
101
			new Request('GET', $this->client->getConfiguration()->getBaseUri() . sprintf('/execution/%s/output/state%s', urlencode($id), $stateOnly ? '?stateOnly=true' : ''))
102
		);
103
	}
104
105
	/**
106
	 * @param string $id
107
	 * @param array $params
108
	 * @return PromiseInterface
109
	 */
110
	public function abort(string $id, array $params = []): PromiseInterface
111
	{
112
		return $this->client->getConfiguration()->getGuzzle()->sendAsync(
113
			new Request(
114
				'GET',
115
				$this->client->getConfiguration()->getBaseUri() . sprintf('/execution/%s/abort', urlencode($id)),
116
				[],
117
				$this->client->getConfiguration()->getFormat()->formatParams($params))
118
		);
119
	}
120
121
	/**
122
	 * @return PromiseInterface
123
	 */
124
	public function enable(): PromiseInterface
125
	{
126
		return $this->client->getConfiguration()->getGuzzle()->sendAsync(
127
			new Request('POST', $this->client->getConfiguration()->getBaseUri() . '/system/executions/enable')
128
		);
129
	}
130
131
	/**
132
	 * @return PromiseInterface
133
	 */
134
	public function disable(): PromiseInterface
135
	{
136
		return $this->client->getConfiguration()->getGuzzle()->sendAsync(
137
			new Request('POST', $this->client->getConfiguration()->getBaseUri() . '/system/executions/disable')
138
		);
139
	}
140
}
141