PluginRequest::updateOrCreate()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 2
nop 2
dl 0
loc 13
ccs 7
cts 8
cp 0.875
crap 4.0312
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Kong\Http\Request;
6
7
use CCT\Kong\Config;
8
use CCT\Kong\Exception\InvalidParameterException;
9
use CCT\Kong\Form\Normalizer\PluginFormNormalizer;
10
use CCT\Kong\Http\Definition\QueryParams;
11
use CCT\Kong\Http\Request;
12
use CCT\Kong\Http\ResponseInterface;
13
use CCT\Kong\Model\Plugin;
14
use JMS\Serializer\SerializationContext;
15
16
class PluginRequest extends Request
17
{
18 13
    protected function setUp()
19
    {
20 13
        if (false === $this->config->has(Config::URI_PREFIX)) {
21 13
            $this->config->set(Config::URI_PREFIX, '/plugins/');
22
        }
23 13
    }
24
25 2
    public function list(QueryParams $queryParams = null): ResponseInterface
26
    {
27 2
        $this->config->set('serialization_context', SerializationContext::create()->setGroups('read'));
28
29 2
        return parent::requestGet($this->getUri(), $queryParams);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::requestGe...getUri(), $queryParams) could return the type Symfony\Component\HttpFoundation\Response which is incompatible with the type-hinted return CCT\Kong\Http\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
30
    }
31
32 1
    public function listEnabled(): ResponseInterface
33
    {
34 1
        $this->config->set('serialization_context', SerializationContext::create()->setGroups('read'));
35 1
        $this->config->set(Config::RESPONSE_TRANSFORMERS, []);
36
37 1
        return parent::requestGet($this->appendToUri('/enabled/'), null);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::requestGe...Uri('/enabled/'), null) could return the type Symfony\Component\HttpFoundation\Response which is incompatible with the type-hinted return CCT\Kong\Http\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
38
    }
39
40 1
    public function listByApi(string $apiId): ResponseInterface
41
    {
42 1
        $this->config->set('serialization_context', SerializationContext::create()->setGroups('read'));
43
44 1
        return parent::requestGet($this->getUriByApiId($apiId), null);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::requestGe...iByApiId($apiId), null) could return the type Symfony\Component\HttpFoundation\Response which is incompatible with the type-hinted return CCT\Kong\Http\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
45
    }
46
47 2
    public function create(Plugin $plugin, string $apiId = null): ResponseInterface
48
    {
49 2
        if (null === $plugin->getApiId() && null === $apiId) {
50
            throw new InvalidParameterException('The API ID is required to create a new Plugin.');
51
        }
52
53 2
        $this->config->set('serialization_context', SerializationContext::create()->setGroups('create'));
54 2
        $this->changeFormNormalizer();
55
56 2
        $apiId = $plugin->getApiId() ?: $apiId;
57 2
        $uri = $this->getUriByApiId($apiId);
0 ignored issues
show
Bug introduced by
It seems like $apiId can also be of type null; however, parameter $apiId of CCT\Kong\Http\Request\Pl...equest::getUriByApiId() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        $uri = $this->getUriByApiId(/** @scrutinizer ignore-type */ $apiId);
Loading history...
58
59 2
        return parent::requestPost($uri, $plugin, null);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::requestPost($uri, $plugin, null) could return the type Symfony\Component\HttpFoundation\Response which is incompatible with the type-hinted return CCT\Kong\Http\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
60
    }
61
62 5
    public function retrieve(string $apiId, string $pluginId): ResponseInterface
63
    {
64 5
        $this->config->set('serialization_context', SerializationContext::create()->setGroups('read'));
65 5
        $uri = $this->getUriByApiId($apiId);
66
67 5
        return parent::requestGet($this->appendToUri($pluginId, $uri), null);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::requestGe...$pluginId, $uri), null) could return the type Symfony\Component\HttpFoundation\Response which is incompatible with the type-hinted return CCT\Kong\Http\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
68
    }
69
70 1
    public function retrieveSchema(string $pluginName): ResponseInterface
71
    {
72 1
        $this->config->set('serialization_context', SerializationContext::create()->setGroups('read'));
73 1
        $this->config->set(Config::RESPONSE_TRANSFORMERS, []);
74
75 1
        $uri = $this->appendToUri(sprintf(
76 1
            '/schema/%s/',
77 1
            $pluginName
78
        ));
79
80 1
        return parent::requestGet($uri, null);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::requestGet($uri, null) could return the type Symfony\Component\HttpFoundation\Response which is incompatible with the type-hinted return CCT\Kong\Http\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
81
    }
82
83 1
    public function update(Plugin $plugin): ResponseInterface
84
    {
85 1
        $this->config->set('serialization_context', SerializationContext::create()->setGroups('update'));
86 1
        $this->changeFormNormalizer();
87
88 1
        $uri = $this->getUriByApiId($plugin->getApiId());
89
90 1
        return parent::requestPatch($this->appendToUri($plugin->getId(), $uri), $plugin, null);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::requestPa..., $uri), $plugin, null) could return the type Symfony\Component\HttpFoundation\Response which is incompatible with the type-hinted return CCT\Kong\Http\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
91
    }
92
93 1
    public function updateOrCreate(Plugin $plugin, string $apiId = null): ResponseInterface
94
    {
95 1
        if (null === $plugin->getApiId() && null === $apiId) {
96
            throw new InvalidParameterException('The API ID is required to create a new Plugin.');
97
        }
98
99 1
        $this->config->set('serialization_context', SerializationContext::create()->setGroups([ 'update', 'create' ]));
100 1
        $this->changeFormNormalizer();
101
102 1
        $apiId = $plugin->getApiId() ?: $apiId;
103 1
        $uri = $this->getUriByApiId($apiId);
0 ignored issues
show
Bug introduced by
It seems like $apiId can also be of type null; however, parameter $apiId of CCT\Kong\Http\Request\Pl...equest::getUriByApiId() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
        $uri = $this->getUriByApiId(/** @scrutinizer ignore-type */ $apiId);
Loading history...
104
105 1
        return parent::requestPut($uri, $plugin, null);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::requestPut($uri, $plugin, null) could return the type Symfony\Component\HttpFoundation\Response which is incompatible with the type-hinted return CCT\Kong\Http\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
106
    }
107
108 1
    public function delete(Plugin $plugin): ResponseInterface
109
    {
110 1
        $this->validateObjectId($plugin);
111 1
        $this->config->set('serialization_context', SerializationContext::create()->setGroups('delete'));
112
113 1
        $uri = $this->getUriByApiId($plugin->getApiId());
114
115 1
        return parent::requestDelete($this->appendToUri($plugin->getId(), $uri), null);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::requestDe...->getId(), $uri), null) could return the type Symfony\Component\HttpFoundation\Response which is incompatible with the type-hinted return CCT\Kong\Http\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
116
    }
117
118
    /**
119
     * @param string $apiId
120
     *
121
     * @return string
122
     */
123 8
    protected function getUriByApiId(string $apiId): string
124
    {
125 8
        return sprintf(
126 8
            '/apis/%s/%s',
127 8
            $apiId,
128 8
            ltrim($this->getUri(), '/')
129
        );
130
    }
131
132
    /**
133
     * Sets the FormNormalizer to use one specific for Plugin.
134
     *
135
     * @return void
136
     */
137 4
    protected function changeFormNormalizer(): void
138
    {
139 4
        $this->config->set(
140 4
            Config::FORM_NORMALIZER,
141 4
            new PluginFormNormalizer(
142 4
                $this->serializer,
143 4
                $this->config->get('serialization_context')
144
            )
145
        );
146 4
    }
147
}
148