FieldService::postField()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Yproximite\Api\Service;
5
6
use Yproximite\Api\Model\Field\Field;
7
use Yproximite\Api\Message\Field\FieldListMessage;
8
use Yproximite\Api\Message\Field\FieldPostMessage;
9
use Yproximite\Api\Message\Field\FieldPatchMessage;
10
use Yproximite\Api\Message\Field\FieldOverrideMessage;
11
12
/**
13
 * Class FieldService
14
 */
15 View Code Duplication
class FieldService extends AbstractService implements ServiceInterface
0 ignored issues
show
Duplication introduced by
This class 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...
16
{
17
    /**
18
     * @param FieldListMessage $message
19
     *
20
     * @return Field[]
21
     */
22
    public function getFields(FieldListMessage $message): array
23
    {
24
        $path = sprintf('sites/%d/fields', $message->getSiteId());
25
26
        $response = $this->getClient()->sendRequest('GET', $path);
27
28
        /** @var Field[] $models */
29
        $models = $this->getModelFactory()->createMany(Field::class, $response);
30
31
        return $models;
32
    }
33
34
    /**
35
     * @param FieldPostMessage $message
36
     *
37
     * @return Field
38
     */
39
    public function postField(FieldPostMessage $message): Field
40
    {
41
        $path = sprintf('sites/%d/fields', $message->getSiteId());
42
        $data = ['api_field' => $message->build()];
43
44
        $response = $this->getClient()->sendRequest('POST', $path, $data);
45
46
        /** @var Field $model */
47
        $model = $this->getModelFactory()->create(Field::class, $response);
48
49
        return $model;
50
    }
51
52
    /**
53
     * @param FieldPatchMessage $message
54
     *
55
     * @return Field
56
     */
57
    public function patchField(FieldPatchMessage $message): Field
58
    {
59
        $path = sprintf('sites/%d/fields/%d', $message->getSiteId(), $message->getId());
60
        $data = ['api_field' => $message->build()];
61
62
        $response = $this->getClient()->sendRequest('PATCH', $path, $data);
63
64
        /** @var Field $model */
65
        $model = $this->getModelFactory()->create(Field::class, $response);
66
67
        return $model;
68
    }
69
70
    /**
71
     * @param FieldOverrideMessage $message
72
     *
73
     * @return Field
74
     */
75
    public function overrideField(FieldOverrideMessage $message): Field
76
    {
77
        $path = sprintf('sites/%d/fields/%d/override', $message->getSiteId(), $message->getId());
78
79
        $response = $this->getClient()->sendRequest('GET', $path);
80
81
        /** @var Field $model */
82
        $model = $this->getModelFactory()->create(Field::class, $response);
83
84
        return $model;
85
    }
86
}
87