Passed
Branch master (d23cf5)
by Jonathan
22:25 queued 01:36
created

DeleteTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 13
c 1
b 0
f 0
dl 0
loc 62
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteTemplate() 0 9 1
A deleteApiKey() 0 9 1
A delete() 0 8 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 © 2023 Text Control GmbH
13
 */
14
15
namespace TextControl\ReportingCloud;
16
17
use Ctw\Http\HttpMethod;
18
use Ctw\Http\HttpStatus;
19
use GuzzleHttp\RequestOptions;
20
use Psr\Http\Message\ResponseInterface;
21
use TextControl\ReportingCloud\Assert\Assert;
22
23
/**
24
 * Trait DeleteTrait
25
 */
26
trait DeleteTrait
27
{
28
    /**
29
     * Delete an API key
30
     */
31 8
    public function deleteApiKey(string $key): bool
32
    {
33 8
        Assert::assertApiKey($key);
34
35 8
        $query = [
36 8
            'key' => $key,
37 8
        ];
38
39 8
        return $this->delete('/account/apikey', $query, '', HttpStatus::STATUS_OK);
40
    }
41
42
    /**
43
     * Delete a template in template storage
44
     */
45 28
    public function deleteTemplate(string $templateName): bool
46
    {
47 28
        Assert::assertTemplateName($templateName);
48
49 26
        $query = [
50 26
            'templateName' => $templateName,
51 26
        ];
52
53 26
        return $this->delete('/templates/delete', $query, '', HttpStatus::STATUS_NO_CONTENT);
54
    }
55
56
    /**
57
     * Construct URI with version number
58
     *
59
     * @param string $uri URI
60
     */
61
    abstract protected function uri(string $uri): string;
62
63
    /**
64
     * Request the URI with options
65
     *
66
     * @param string $method  HTTP method
67
     * @param string $uri     URI
68
     * @param array  $options Options
69
     */
70
    abstract protected function request(string $method, string $uri, array $options): ResponseInterface;
71
72
    /**
73
     * Execute a DELETE request via REST client
74
     *
75
     * @param string $uri        URI
76
     * @param array  $query      Query
77
     * @param mixed  $json       JSON
78
     * @param int    $statusCode Required HTTP status code for response
79
     */
80 34
    private function delete(string $uri, array $query = [], mixed $json = '', int $statusCode = 0): bool
81
    {
82 34
        $response = $this->request(HttpMethod::METHOD_DELETE, $this->uri($uri), [
83 34
            RequestOptions::QUERY => $query,
84 34
            RequestOptions::JSON  => $json,
85 34
        ]);
86
87 32
        return $statusCode === $response->getStatusCode();
88
    }
89
}
90