Passed
Branch development-2.0 (93a445)
by Jonathan
13:09
created

DeleteTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 17
dl 0
loc 102
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteTemplate() 0 9 1
A deleteApiKey() 0 9 1
A delete() 0 20 2
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\RequestOptions;
18
use TxTextControl\ReportingCloud\Assert\Assert;
19
use TxTextControl\ReportingCloud\StatusCode\StatusCode;
20
21
/**
22
 * Trait DeleteTrait
23
 *
24
 * @package TxTextControl\ReportingCloud
25
 * @author  Jonathan Maron (@JonathanMaron)
26
 */
27
trait DeleteTrait
28
{
29
    /**
30
     * Abstract Methods
31
     * -----------------------------------------------------------------------------------------------------------------
32
     */
33
34
    /**
35
     * Construct URI with version number
36
     *
37
     * @param string $uri URI
38
     *
39
     * @return string
40
     */
41
    abstract protected function uri(string $uri): string;
42
43
    /**
44
     * Request the URI with options
45
     *
46
     * @param string $method  HTTP method
47
     * @param string $uri     URI
48
     * @param array  $options Options
49
     *
50
     * @return mixed|null|\Psr\Http\Message\ResponseInterface
51
     *
52
     * @throws RuntimeException
53
     */
54
    abstract protected function request(string $method, string $uri, array $options);
55
56
    /**
57
     * DELETE Methods
58
     * -----------------------------------------------------------------------------------------------------------------
59
     */
60
61
    /**
62
     * Delete an API key
63
     *
64
     * @param string $key
65
     *
66
     * @return bool
67
     * @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException
68
     */
69 2
    public function deleteApiKey(string $key): bool
70
    {
71 2
        Assert::assertApiKey($key);
72
73
        $query = [
74 2
            'key' => $key,
75
        ];
76
77 2
        return $this->delete('/account/apikey', $query, null, StatusCode::OK);
78
    }
79
80
    /**
81
     * Delete a template in template storage
82
     *
83
     * @param string $templateName
84
     *
85
     * @return bool
86
     * @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException
87
     */
88 11
    public function deleteTemplate(string $templateName): bool
89
    {
90 11
        Assert::assertTemplateName($templateName);
91
92
        $query = [
93 10
            'templateName' => $templateName,
94
        ];
95
96 10
        return $this->delete('/templates/delete', $query, null, StatusCode::NO_CONTENT);
97
    }
98
99
    /**
100
     * Execute a DELETE request via REST client
101
     *
102
     * @param string       $uri        URI
103
     * @param array        $query      Query
104
     * @param string|array $json       JSON
105
     * @param int          $statusCode Required HTTP status code for response
106
     *
107
     * @return bool
108
     */
109 12
    private function delete(
110
        string $uri,
111
        ?array $query = null,
112
        $json = null,
113
        ?int $statusCode = null
114
    ): bool {
115 12
        $ret = false;
116
117
        $options = [
118 12
            RequestOptions::QUERY => $query,
119 12
            RequestOptions::JSON  => $json,
120
        ];
121
122 12
        $response = $this->request('DELETE', $this->uri($uri), $options);
123
124 11
        if ($statusCode === $response->getStatusCode()) {
125 11
            $ret = true;
126
        }
127
128 11
        return $ret;
129
    }
130
}
131