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

MediaService::getMediaDynamicUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Yproximite\Api\Service;
5
6
use Yproximite\Api\Model\Media\Media;
7
use Yproximite\Api\Message\Media\MediaUploadMessage;
8
use Yproximite\Api\Message\Media\MediaDynamicUrlMessage;
9
10
/**
11
 * Class MediaService
12
 */
13
class MediaService extends AbstractService implements ServiceInterface
14
{
15
    /**
16
     * @param MediaDynamicUrlMessage $message
17
     *
18
     * @return string
19
     */
20
    public function getMediaDynamicUrl(MediaDynamicUrlMessage $message): string
21
    {
22
        $path = sprintf(
23
            'sites/%d/media/%d/dynamic_url/%s',
24
            $message->getSiteId(),
25
            $message->getId(),
26
            $message->getFormat()
27
        );
28
29
        $response = $this->getClient()->sendRequest('GET', $path);
30
31
        return (string) $response['url'];
32
    }
33
34
    /**
35
     * @param MediaUploadMessage $message
36
     *
37
     * @return Media[]
38
     */
39
    public function uploadMedias(MediaUploadMessage $message): array
40
    {
41
        $message->initBuilder();
42
43
        $path    = sprintf('sites/%d/uploads/media', $message->getSiteId());
44
        $body    = $message->build();
45
        $headers = $message->buildHeaders();
46
47
        $response = $this->getClient()->sendRequest('POST', $path, $body, $headers);
48
49
        /** @var Media[] $model */
50
        $model = $this->getModelFactory()->createMany(Media::class, $response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $this->getClient()->send...$path, $body, $headers) on line 47 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...
51
52
        return $model;
53
    }
54
}
55