DeleteTrait::deleteTemplate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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