Passed
Branch development-2.0 (a08853)
by Jonathan
04:00
created

DeleteTrait::deleteApiKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP Wrapper
6
 *
7
 * PHP wrapper 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://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12
 * @copyright © 2019 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud;
16
17
use GuzzleHttp\Psr7\Response;
18
use GuzzleHttp\RequestOptions;
19
use TxTextControl\ReportingCloud\Assert\Assert;
20
use TxTextControl\ReportingCloud\StatusCode\StatusCode;
21
22
/**
23
 * Trait DeleteTrait
24
 *
25
 * @package TxTextControl\ReportingCloud
26
 * @author  Jonathan Maron (@JonathanMaron)
27
 */
28
trait DeleteTrait
29
{
30
    /**
31
     * Abstract Methods
32
     * -----------------------------------------------------------------------------------------------------------------
33
     */
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 mixed|null|\Psr\Http\Message\ResponseInterface
52
     *
53
     * @throws RuntimeException
54
     */
55
    abstract protected function request(string $method, string $uri, array $options);
56
57
    /**
58
     * DELETE Methods
59
     * -----------------------------------------------------------------------------------------------------------------
60
     */
61
62
    /**
63
     * Delete an API key
64
     *
65
     * @param string $key
66
     *
67
     * @return bool
68
     * @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException
69
     */
70 2
    public function deleteApiKey(string $key): bool
71
    {
72 2
        Assert::assertApiKey($key);
73
74
        $query = [
75 2
            'key' => $key,
76
        ];
77
78 2
        return $this->delete('/account/apikey', $query);
79
    }
80
81
    /**
82
     * Delete a template in template storage
83
     *
84
     * @param string $templateName
85
     *
86
     * @return bool
87
     * @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException
88
     */
89 11
    public function deleteTemplate(string $templateName): bool
90
    {
91 11
        Assert::assertTemplateName($templateName);
92
93
        $query = [
94 10
            'templateName' => $templateName,
95
        ];
96
97 10
        return $this->delete('/templates/delete', $query);
98
    }
99
100
    /**
101
     * Execute a DELETE request via REST client
102
     *
103
     * @param string $uri   URI
104
     * @param array  $query Query
105
     *
106
     * @return bool
107
     */
108 12
    private function delete(string $uri, array $query = [])
109
    {
110
        $options = [
111 12
            RequestOptions::QUERY => $query,
112
        ];
113
114
        $statusCodes = [
115 12
            StatusCode::OK,
116 12
            StatusCode::NO_CONTENT,
117
        ];
118
119 12
        $response = $this->request('DELETE', $this->uri($uri), $options);
120
121 11
        if (!$response instanceof Response) {
122
            return false;
123
        }
124
125 11
        if (!in_array($response->getStatusCode(), $statusCodes)) {
126
            return false;
127
        }
128
129 11
        return true;
130
    }
131
}
132