Passed
Branch development-2.0 (93a445)
by Jonathan
13:09
created

PutTrait::put()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 20
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 4
crap 2
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\RequestOptions;
18
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
19
use TxTextControl\ReportingCloud\StatusCode\StatusCode;
20
21
/**
22
 * Trait PutTrait
23
 *
24
 * @package TxTextControl\ReportingCloud
25
 * @author  Jonathan Maron (@JonathanMaron)
26
 */
27
trait PutTrait
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(string $uri): string;
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(string $method, string $uri, array $options);
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 3
    public function createApiKey(): string
78
    {
79 3
        return $this->put('/account/apikey', null, null, StatusCode::CREATED);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->put('/acco...de\StatusCode::CREATED) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
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|null
91
     */
92 3
    private function put(
93
        string $uri,
94
        ?array $query = null,
95
        $json = null,
96
        ?int $statusCode = null
97
    ) {
98 3
        $ret = null;
99
100
        $options = [
101 3
            RequestOptions::QUERY => $query,
102 3
            RequestOptions::JSON  => $json,
103
        ];
104
105 3
        $response = $this->request('PUT', $this->uri($uri), $options);
106
107 3
        if ($statusCode === $response->getStatusCode()) {
108 3
            $ret = (string) json_decode($response->getBody()->getContents());
109
        }
110
111 3
        return $ret;
112
    }
113
}
114