Passed
Branch master (81c7db)
by Jonathan
10:52 queued 01:40
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
eloc 9
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
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
/**
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 4
    public function deleteApiKey($key)
69
    {
70 4
        $ret = false;
71
72 4
        StaticValidator::execute($key, 'ApiKey');
73
74
        $options = [
75 4
            RequestOptions::QUERY => [
76 4
                'key' => $key,
77 2
            ],
78 2
        ];
79
80 4
        $response = $this->request('DELETE', $this->uri('/account/apikey'), $options);
81
82 4
        if ($response instanceof Response && 200 === $response->getStatusCode()) {
83 4
            $ret = true;
84 2
        }
85
86 4
        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 22
    public function deleteTemplate($templateName)
99
    {
100 22
        $ret = false;
101
102 22
        StaticValidator::execute($templateName, 'TemplateName');
103
104
        $options = [
105 20
            RequestOptions::QUERY => [
106 20
                'templateName' => $templateName,
107 10
            ],
108 10
        ];
109
110 20
        $response = $this->request('DELETE', $this->uri('/templates/delete'), $options);
111
112 18
        if ($response instanceof Response && 204 === $response->getStatusCode()) {
113 18
            $ret = true;
114 9
        }
115
116 18
        return $ret;
117
    }
118
}
119