Passed
Push — master ( fbc447...748ca4 )
by Jonathan
18:21
created

PutTrait::createApiKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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
     * @throws \TxTextControl\ReportingCloud\Exception\RuntimeException
53
     */
54
    abstract protected function request(string $method, string $uri, array $options): Response;
55
56
    /**
57
     * Using the passed propertyMap, recursively build array
58
     *
59
     * @param array       $array       Array
60
     * @param PropertyMap $propertyMap PropertyMap
61
     *
62
     * @return array
63
     */
64
    abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array;
65
66
    /**
67
     * PUT Methods
68
     * -----------------------------------------------------------------------------------------------------------------
69
     */
70
71
    /**
72
     * Create an API key
73
     *
74
     * @return string
75
     * @throws \TxTextControl\ReportingCloud\Exception\InvalidArgumentException
76
     */
77 9
    public function createApiKey(): string
78
    {
79 9
        return $this->put('/account/apikey', null, null, StatusCode::CREATED);
80
    }
81
82
    /**
83
     * Execute a PUT request via REST client
84
     *
85
     * @param string       $uri        URI
86
     * @param array        $query      Query
87
     * @param string|array $json       JSON
88
     * @param int          $statusCode Required HTTP status code for response
89
     *
90
     * @return string
91
     */
92 9
    private function put(
93
        string $uri,
94
        ?array $query = null,
95
        $json = null,
96
        ?int $statusCode = null
97
    ) {
98 9
        $ret = '';
99
100
        $options = [
101 9
            RequestOptions::QUERY => $query,
102 9
            RequestOptions::JSON  => $json,
103
        ];
104
105 9
        $response = $this->request('PUT', $this->uri($uri), $options);
106
107 9
        if ($statusCode === $response->getStatusCode()) {
108 9
            $ret = (string) json_decode($response->getBody()->getContents());
109
        }
110
111 9
        return $ret;
112
    }
113
}
114