Upload::getParameters()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php declare(strict_types = 1);
2
3
namespace PeeHaa\AsyncTwitter\Api\Request\Media;
4
5
use PeeHaa\AsyncTwitter\Api\Request\Media\Response\UploadResponse;
6
use PeeHaa\AsyncTwitter\Exception;
7
use PeeHaa\AsyncTwitter\Request\FieldParameter;
8
use PeeHaa\AsyncTwitter\Request\FileParameter;
9
10
/**
11
 * @link https://dev.twitter.com/rest/reference/post/media/upload-append
12
 */
13
class Upload extends BaseRequest
14
{
15
    const METHOD   = 'POST';
16
    const ENDPOINT = '/media/upload.json';
17
18
    public function __construct()
19
    {
20
        parent::__construct(self::METHOD, self::ENDPOINT);
21
    }
22
23
    public function setAdditionalOwners(array $userIds)
24
    {
25
        if (count($userIds) > 100) {
26
            throw new Exception('A maximum of 100 addition owners can be specified for a media resource');
27
        }
28
29
        foreach ($userIds as $id) {
30
            if (!\ctype_digit((string)$id)) {
31
                throw new Exception('User IDs must be numeric');
32
            }
33
        }
34
35
        $this->parameters['additional_owners'] = implode(',', $userIds);
36
37
        return $this;
38
    }
39
40
    public function getParameters(): array
41
    {
42
        $parameters = [new FileParameter('media', $this->parameters['file_path'])];
43
44
        if (isset($this->parameters['additional_owners'])) {
45
            $parameters[] = new FieldParameter('additional_owners', $this->parameters['additional_owners']);
46
        }
47
48
        return $parameters;
49
    }
50
51
    public function handleResponse(array $responseData)
52
    {
53
        $mediaId = $responseData['media_id_string'] ?? '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
54
        $size = (int)($responseData['size'] ?? 0);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
55
        $expiryTime = new \DateTimeImmutable('@' . (time() + ($responseData['expires_after_secs'] ?? 0)));
56
        $mimeType = $responseData['image']['image_type'] ?? '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
57
        $width = (int)($responseData['image']['w'] ?? 0);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
58
        $height = (int)($responseData['image']['h'] ?? 0);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
59
60
        return new UploadResponse($mediaId, $size, $expiryTime, $mimeType, $width, $height);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \PeeHaa\Async...Type, $width, $height); (PeeHaa\AsyncTwitter\Api\...Response\UploadResponse) is incompatible with the return type of the parent method PeeHaa\AsyncTwitter\Api\...Request::handleResponse of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
61
    }
62
}
63