RequestFactory::getCreateRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Kwk\Geckoboard\Dataset;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Message\RequestInterface;
7
8
class RequestFactory
9
{
10
    /**
11
     * @var ClientInterface
12
     */
13
    private $client;
14
15
    /**
16
     * @var string
17
     */
18
    private $apiKey;
19
20
    /**
21
     * @param ClientInterface $client
22
     * @param string          $apiKey
23
     */
24 4
    public function __construct(ClientInterface $client, $apiKey)
25
    {
26 4
        $this->apiKey = $apiKey;
27 4
        $this->client = $client;
28 4
    }
29
30
    /**
31
     * @param DataSetInterface $dataSet
32
     *
33
     * @return RequestInterface
34
     */
35 1
    public function getCreateRequest(DataSetInterface $dataSet)
36
    {
37 1
        return $this->client->createRequest(
38 1
            'PUT',
39 1
            sprintf('/datasets/%s', $dataSet->getName()),
40
            [
41 1
                'headers' => [
42
                    'Content-Type' => 'application/json',
43
                ],
44 1
                'auth'    => [$this->apiKey, ''],
45 1
                'json'    => $dataSet->getDefinition(),
46
            ]
47
        );
48
    }
49
50
    /**
51
     * @param string                $datasetName
52
     * @param DataSetRowInterface[] $rows
53
     *
54
     * @return RequestInterface
55
     */
56 1 View Code Duplication
    public function getAppendRequest($datasetName, array $rows)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58 1
        $data = [];
59 1
        foreach ($rows as $row) {
60 1
            $data[] = $row->getData();
61
        }
62
63 1
        return $this->client->createRequest(
64 1
            'POST',
65 1
            sprintf('/datasets/%s/data', $datasetName),
66
            [
67 1
                'headers' => [
68
                    'Content-Type' => 'application/json',
69
                ],
70 1
                'auth'    => [$this->apiKey, ''],
71 1
                'json'    => ['data' => $data],
72
            ]
73
        );
74
    }
75
76
    /**
77
     * @param string                $datasetName
78
     * @param DataSetRowInterface[] $rows
79
     *
80
     * @return RequestInterface
81
     */
82 1 View Code Duplication
    public function getReplaceRequest($datasetName, array $rows)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84 1
        $data = [];
85 1
        foreach ($rows as $row) {
86 1
            $data[] = $row->getData();
87
        }
88
89 1
        return $this->client->createRequest(
90 1
            'PUT',
91 1
            sprintf('/datasets/%s/data', $datasetName),
92
            [
93 1
                'headers' => [
94
                    'Content-Type' => 'application/json',
95
                ],
96 1
                'auth'    => [$this->apiKey, ''],
97 1
                'json'    => ['data' => $data],
98
            ]
99
        );
100
    }
101
102
    /**
103
     * @param string $datasetName
104
     *
105
     * @return RequestInterface
106
     */
107 1
    public function getDeleteRequest($datasetName)
108
    {
109 1
        return $this->client->createRequest(
110 1
            'DELETE',
111 1
            sprintf('/datasets/%s', $datasetName),
112
            [
113 1
                'auth' => [$this->apiKey, ''],
114
            ]
115
        );
116
    }
117
}
118