Test Failed
Push — master ( 35b555...9b1d30 )
by Jonathan
08:17
created

DeleteTrait::deleteTemplate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.3332

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 19
ccs 8
cts 12
cp 0.6667
rs 9.9666
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3.3332
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
/**
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($uri);
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($method, $uri, $options);
55
56
    /**
57
     * DELETE Methods
58
     * -----------------------------------------------------------------------------------------------------------------
59
     */
60
61
    /**
62
     * Delete an API key
63
     *
64
     * @param string $key API key
65
     *
66
     * @return bool
67
     */
68
    public function deleteApiKey($key)
69
    {
70
        $ret = false;
71
72
        StaticValidator::execute($key, 'ApiKey');
73
74
        $options = [
75
            RequestOptions::QUERY => [
76
                'key' => $key,
77
            ],
78
        ];
79
80
        $response = $this->request('DELETE', $this->uri('/account/apikey'), $options);
81
82
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
83
            $ret = true;
84
        }
85
86
        return $ret;
87
    }
88
89
    /**
90
     * Delete a template in template storage
91
     *
92
     * @param string $templateName Template name
93
     *
94
     * @throws InvalidArgumentException
95
     *
96
     * @return bool
97
     */
98 4
    public function deleteTemplate($templateName)
99
    {
100 4
        $ret = false;
101
102 4
        StaticValidator::execute($templateName, 'TemplateName');
103
104
        $options = [
105 2
            RequestOptions::QUERY => [
106 2
                'templateName' => $templateName,
107 1
            ],
108 1
        ];
109
110 2
        $response = $this->request('DELETE', $this->uri('/templates/delete'), $options);
111
112
        if ($response instanceof Response && 204 === $response->getStatusCode()) {
113
            $ret = true;
114
        }
115
116
        return $ret;
117
    }
118
}
119