PutTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 3
eloc 11
c 8
b 0
f 0
dl 0
loc 82
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createApiKey() 0 3 1
A put() 0 22 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP SDK
6
 *
7
 * PHP SDK 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://git.io/Jejj2 for the canonical source repository
11
 * @license   https://git.io/Jejjr
12
 * @copyright © 2022 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud;
16
17
use Ctw\Http\HttpMethod;
18
use Ctw\Http\HttpStatus;
19
use GuzzleHttp\RequestOptions;
20
use Psr\Http\Message\ResponseInterface;
21
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
22
use TxTextControl\ReportingCloud\Exception\RuntimeException;
23
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
24
25
/**
26
 * Trait PutTrait
27
 *
28
 * @package TxTextControl\ReportingCloud
29
 * @author  Jonathan Maron (@JonathanMaron)
30
 */
31
trait PutTrait
32
{
33
    // <editor-fold desc="Abstract methods">
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 ResponseInterface
52
     * @throws RuntimeException
53
     */
54
    abstract protected function request(string $method, string $uri, array $options): ResponseInterface;
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
    // </editor-fold>
67
68
    // <editor-fold desc="Methods">
69
70
    /**
71
     * Create an API key
72
     *
73
     * @return string
74
     * @throws InvalidArgumentException
75
     */
76 10
    public function createApiKey(): string
77
    {
78 10
        return $this->put('/account/apikey', [], '', HttpStatus::STATUS_CREATED);
79
    }
80
81
    /**
82
     * Execute a PUT request via REST client
83
     *
84
     * @param string  $uri        URI
85
     * @param array[] $query      Query
86
     * @param mixed   $json       JSON
87
     * @param int     $statusCode Required HTTP status code for response
88
     *
89
     * @return string
90
     */
91 10
    private function put(
92
        string $uri,
93
        array $query = [],
94
        $json = '',
95
        int $statusCode = 0
96
    ): string {
97
98 10
        $ret = '';
99
100 10
        $options = [
101 10
            RequestOptions::QUERY => $query,
102 10
            RequestOptions::JSON  => $json,
103 10
        ];
104
105 10
        $response = $this->request(HttpMethod::METHOD_PUT, $this->uri($uri), $options);
106
107 10
        if ($statusCode === $response->getStatusCode()) {
108 10
            $ret = json_decode($response->getBody()->getContents(), true);
109 10
            assert(is_string($ret));
110
        }
111
112 10
        return $ret;
113
    }
114
115
    // </editor-fold>
116
}
117