This repository was archived by the owner on Sep 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDeleteTrait.php
More file actions
126 lines (107 loc) · 3.01 KB
/
Copy pathDeleteTrait.php
File metadata and controls
126 lines (107 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
/**
* ReportingCloud PHP SDK
*
* PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
*
* @link https://www.reporting.cloud to learn more about ReportingCloud
* @link https://git.io/Jejj2 for the canonical source repository
* @license https://git.io/Jejjr
* @copyright © 2022 Text Control GmbH
*/
namespace TxTextControl\ReportingCloud;
use Ctw\Http\HttpMethod;
use Ctw\Http\HttpStatus;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;
use TxTextControl\ReportingCloud\Assert\Assert;
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
use TxTextControl\ReportingCloud\Exception\RuntimeException;
/**
* Trait DeleteTrait
*
* @package TxTextControl\ReportingCloud
* @author Jonathan Maron (@JonathanMaron)
*/
trait DeleteTrait
{
// <editor-fold desc="Abstract methods">
/**
* Construct URI with version number
*
* @param string $uri URI
*
* @return string
*/
abstract protected function uri(string $uri): string;
/**
* Request the URI with options
*
* @param string $method HTTP method
* @param string $uri URI
* @param array $options Options
*
* @return ResponseInterface
* @throws RuntimeException
*/
abstract protected function request(string $method, string $uri, array $options): ResponseInterface;
// </editor-fold>
// <editor-fold desc="Methods">
/**
* Delete an API key
*
* @param string $key
*
* @return bool
* @throws InvalidArgumentException
*/
public function deleteApiKey(string $key): bool
{
Assert::assertApiKey($key);
$query = [
'key' => $key,
];
return $this->delete('/account/apikey', $query, '', HttpStatus::STATUS_OK);
}
/**
* Delete a template in template storage
*
* @param string $templateName
*
* @return bool
* @throws InvalidArgumentException
*/
public function deleteTemplate(string $templateName): bool
{
Assert::assertTemplateName($templateName);
$query = [
'templateName' => $templateName,
];
return $this->delete('/templates/delete', $query, '', HttpStatus::STATUS_NO_CONTENT);
}
/**
* Execute a DELETE request via REST client
*
* @param string $uri URI
* @param array $query Query
* @param mixed $json JSON
* @param int $statusCode Required HTTP status code for response
*
* @return bool
*/
private function delete(
string $uri,
array $query = [],
$json = '',
int $statusCode = 0
): bool {
$options = [
RequestOptions::QUERY => $query,
RequestOptions::JSON => $json,
];
$response = $this->request(HttpMethod::METHOD_DELETE, $this->uri($uri), $options);
return $statusCode === $response->getStatusCode();
}
// </editor-fold>
}