Completed
Push — master ( f95229...017b93 )
by Nikita
13:15
created

SiteService::getSite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Yproximite\Api\Service;
5
6
use Yproximite\Api\Model\Site\Site;
7
use Yproximite\Api\Message\Site\SitePostMessage;
8
use Yproximite\Api\Message\Site\PlatformChildrenListMessage;
9
10
/**
11
 * Class SiteService
12
 */
13
class SiteService extends AbstractService implements ServiceInterface
14
{
15
    /**
16
     * @return Site[]
17
     */
18 View Code Duplication
    public function getSites(): array
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...
19
    {
20
        $path = 'sites';
21
22
        $response = $this->getClient()->sendRequest('GET', $path);
23
24
        /** @var Site[] $models */
25
        $models = $this->getModelFactory()->createMany(Site::class, $response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $this->getClient()->sendRequest('GET', $path) on line 22 can also be of type null; however, Yproximite\Api\Factory\ModelFactory::createMany() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
26
27
        return $models;
28
    }
29
30
    /**
31
     * @param int $id
32
     *
33
     * @return Site
34
     */
35 View Code Duplication
    public function getSite(int $id): Site
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...
36
    {
37
        $path = sprintf('sites/%d', $id);
38
39
        $response = $this->getClient()->sendRequest('GET', $path);
40
41
        /** @var Site $model */
42
        $model = $this->getModelFactory()->create(Site::class, $response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $this->getClient()->sendRequest('GET', $path) on line 39 can also be of type null; however, Yproximite\Api\Factory\ModelFactory::create() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
43
44
        return $model;
45
    }
46
47
    /**
48
     * @param SitePostMessage $message
49
     *
50
     * @return Site
51
     */
52
    public function postSite(SitePostMessage $message): Site
53
    {
54
        $path = 'sites';
55
        $data = ['api_site' => $message->build()];
56
57
        $response = $this->getClient()->sendRequest('POST', $path, $data);
58
59
        /** @var Site $model */
60
        $model = $this->getModelFactory()->create(Site::class, $response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $this->getClient()->send...t('POST', $path, $data) on line 57 can also be of type null; however, Yproximite\Api\Factory\ModelFactory::create() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
61
62
        return $model;
63
    }
64
65
    /**
66
     * @param PlatformChildrenListMessage $message
67
     *
68
     * @return Site[]
69
     */
70 View Code Duplication
    public function getPlatformChildren(PlatformChildrenListMessage $message): array
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...
71
    {
72
        $path = sprintf('platform/%d/children', $message->getSiteId());
73
74
        $response = $this->getClient()->sendRequest('GET', $path);
75
76
        /** @var Site[] $models */
77
        $models = $this->getModelFactory()->createMany(Site::class, $response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $this->getClient()->sendRequest('GET', $path) on line 74 can also be of type null; however, Yproximite\Api\Factory\ModelFactory::createMany() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
78
79
        return $models;
80
    }
81
}
82