Passed
Push — master ( 72713e...d5a1de )
by Jonathan
09:55
created

DeleteTrait::deleteTemplate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 9.4285
cc 3
eloc 9
nc 2
nop 1
crap 3
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2018 Text Control GmbH
12
 */
13
14
namespace TxTextControl\ReportingCloud;
15
16
use GuzzleHttp\Psr7\Response;
17
use GuzzleHttp\RequestOptions;
18
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
19
use TxTextControl\ReportingCloud\Validator\StaticValidator;
20
21
trait DeleteTrait
22
{
23
    /**
24
     * Construct URI with version number
25
     *
26
     * @param string $uri URI
27
     *
28
     * @return string
29
     */
30
    abstract protected function uri($uri);
31
32
    /**
33
     * Request the URI with options
34
     *
35
     * @param string $method  HTTP method
36
     * @param string $uri     URI
37
     * @param array  $options Options
38
     *
39
     * @return mixed|null|\Psr\Http\Message\ResponseInterface
40
     *
41
     * @throws RuntimeException
42
     */
43
    abstract protected function request($method, $uri, $options);
44
45
    /**
46
     * Delete an API key
47
     *
48
     * @param string $key API key
49
     *
50
     * @return bool
51
     */
52 4
    public function deleteApiKey($key)
53
    {
54 4
        $ret = false;
55
56 4
        StaticValidator::execute($key, 'ApiKey');
57
58
        $options = [
59 4
            RequestOptions::QUERY => [
60 4
                'key' => $key,
61 2
            ],
62 2
        ];
63
64 4
        $response = $this->request('DELETE', $this->uri('/account/apikey'), $options);
65
66 4
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
67 4
            $ret = true;
68 2
        }
69
70 4
        return $ret;
71
    }
72
73
    /**
74
     * Delete a template in template storage
75
     *
76
     * @param string $templateName Template name
77
     *
78
     * @throws InvalidArgumentException
79
     *
80
     * @return bool
81
     */
82 22
    public function deleteTemplate($templateName)
83
    {
84 22
        $ret = false;
85
86 22
        StaticValidator::execute($templateName, 'TemplateName');
87
88
        $options = [
89 20
            RequestOptions::QUERY => [
90 20
                'templateName' => $templateName,
91 10
            ],
92 10
        ];
93
94 20
        $response = $this->request('DELETE', $this->uri('/templates/delete'), $options);
95
96 18
        if ($response instanceof Response && 204 === $response->getStatusCode()) {
97 18
            $ret = true;
98 9
        }
99
100 18
        return $ret;
101
    }
102
}
103