Passed
Branch development-2.0 (a08853)
by Jonathan
04:00
created

PutTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 4
eloc 12
dl 0
loc 84
ccs 9
cts 11
cp 0.8182
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A put() 0 21 3
A createApiKey() 0 3 1
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\Psr7\Response;
18
use GuzzleHttp\RequestOptions;
19
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
20
use TxTextControl\ReportingCloud\StatusCode\StatusCode;
21
22
/**
23
 * Trait PutTrait
24
 *
25
 * @package TxTextControl\ReportingCloud
26
 * @author  Jonathan Maron (@JonathanMaron)
27
 */
28
trait PutTrait
29
{
30
    /**
31
     * Abstract Methods
32
     * -----------------------------------------------------------------------------------------------------------------
33
     */
34
35
    /**
36
     * Construct URI with version number
37
     *
38
     * @param string $uri URI
39
     *
40
     * @return string
41
     */
42
    abstract protected function uri(string $uri): string;
43
44
    /**
45
     * Request the URI with options
46
     *
47
     * @param string $method  HTTP method
48
     * @param string $uri     URI
49
     * @param array  $options Options
50
     *
51
     * @return mixed|null|\Psr\Http\Message\ResponseInterface
52
     *
53
     * @throws RuntimeException
54
     */
55
    abstract protected function request(string $method, string $uri, array $options);
56
57
    /**
58
     * Using the passed propertyMap, recursively build array
59
     *
60
     * @param array       $array       Array
61
     * @param PropertyMap $propertyMap PropertyMap
62
     *
63
     * @return array
64
     */
65
    abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array;
66
67
    /**
68
     * PUT Methods
69
     * -----------------------------------------------------------------------------------------------------------------
70
     */
71
72
    /**
73
     * Create an API key
74
     *
75
     * @return string
76
     * @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException
77
     */
78 3
    public function createApiKey(): string
79
    {
80 3
        return $this->put('/account/apikey');
81
    }
82
83
    /**
84
     * Execute a PUT request via REST client
85
     *
86
     * @param string $uri   URI
87
     * @param array  $query Query
88
     *
89
     * @return string
90
     */
91 3
    private function put(string $uri, array $query = [])
92
    {
93
        $options = [
94 3
            RequestOptions::QUERY => $query,
95
        ];
96
97
        $statusCodes = [
98 3
            StatusCode::CREATED,
99
        ];
100
101 3
        $response = $this->request('PUT', $this->uri($uri), $options);
102
103 3
        if (!$response instanceof Response) {
104
            return '';
105
        }
106
107 3
        if (!in_array($response->getStatusCode(), $statusCodes)) {
108
            return '';
109
        }
110
111 3
        return (string) json_decode($response->getBody()->getContents());
112
    }
113
}
114