Passed
Branch master (81c7db)
by Jonathan
10:52 queued 01:40
created

PutTrait::createApiKey()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
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 TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
18
use TxTextControl\ReportingCloud\Validator\StaticValidator;
19
20
/**
21
 * Trait PutTrait
22
 *
23
 * @package TxTextControl\ReportingCloud
24
 * @author  Jonathan Maron (@JonathanMaron)
25
 */
26
trait PutTrait
27
{
28
    /**
29
     * Abstract Methods
30
     * -----------------------------------------------------------------------------------------------------------------
31
     */
32
33
    /**
34
     * Construct URI with version number
35
     *
36
     * @param string $uri URI
37
     *
38
     * @return string
39
     */
40
    abstract protected function uri($uri);
41
42
    /**
43
     * Request the URI with options
44
     *
45
     * @param string $method  HTTP method
46
     * @param string $uri     URI
47
     * @param array  $options Options
48
     *
49
     * @return mixed|null|\Psr\Http\Message\ResponseInterface
50
     *
51
     * @throws RuntimeException
52
     */
53
    abstract protected function request($method, $uri, $options);
54
55
    /**
56
     * Using the passed propertyMap, recursively build array
57
     *
58
     * @param array       $array       Array
59
     * @param PropertyMap $propertyMap PropertyMap
60
     *
61
     * @return array
62
     */
63
    abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap);
64
65
    /**
66
     * PUT Methods
67
     * -----------------------------------------------------------------------------------------------------------------
68
     */
69
70
    /**
71
     * Create an API key
72
     *
73
     * @return string|null
74
     */
75 6
    public function createApiKey()
76
    {
77 6
        $ret = null;
78
79 6
        $response = $this->request('PUT', $this->uri('/account/apikey'), []);
80
81 6
        if ($response instanceof Response && 201 === $response->getStatusCode()) {
82 6
            $ret = (string) json_decode($response->getBody(), true);
83 6
            StaticValidator::execute($ret, 'ApiKey');
84 3
        }
85
86 6
        return $ret;
87
    }
88
}
89