Completed
Push — master ( 8bcfe7...12792b )
by Rémi
03:26
created

TwitterMediaSerializer::canSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Twitter\Serializer;
4
5
use Twitter\Object\TwitterMedia;
6
use Twitter\TwitterSerializable;
7
use Twitter\TwitterSerializer;
8
9
class TwitterMediaSerializer implements TwitterSerializer
10
{
11
    /**
12
     * @var TwitterEntityIndicesSerializer
13
     */
14
    private $entityIndicesSerializer;
15
16
    /**
17
     * @var TwitterMediaSizeSerializer
18
     */
19
    private $mediaSizeSerializer;
20
21
    /**
22
     * Constructor
23
     *
24
     * @param TwitterEntityIndicesSerializer $entityIndicesSerializer
25
     * @param TwitterMediaSizeSerializer     $mediaSizeSerializer
26
     */
27 30
    public function __construct(
28
        TwitterEntityIndicesSerializer $entityIndicesSerializer,
29
        TwitterMediaSizeSerializer $mediaSizeSerializer
30
    ) {
31 30
        $this->entityIndicesSerializer  = $entityIndicesSerializer;
32 30
        $this->mediaSizeSerializer = $mediaSizeSerializer;
33 30
    }
34
35
    /**
36
     * @param  TwitterSerializable $object
37
     * @return \stdClass
38
     */
39 6
    public function serialize(TwitterSerializable $object)
40
    {
41 6
        if (!$this->canSerialize($object)) {
42 3
            throw new \InvalidArgumentException('$object must be an instance of TwitterMedia');
43
        }
44
45 3
        $media = new \stdClass();
46 3
        $media->id = $object->getId();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getId() does only exist in the following implementations of said interface: Twitter\Object\AbstractMessage, Twitter\Object\AbstractTwitterMedia, Twitter\Object\Tweet, Twitter\Object\TwitterDelete, Twitter\Object\TwitterDirectMessage, Twitter\Object\TwitterExtendedEntity, Twitter\Object\TwitterMedia, Twitter\Object\TwitterUser, Twitter\Object\TwitterUserMention.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
47 3
        $media->media_url = $object->getMediaUrl();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getMediaUrl() does only exist in the following implementations of said interface: Twitter\Object\AbstractTwitterMedia, Twitter\Object\TwitterExtendedEntity, Twitter\Object\TwitterMedia.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
48 3
        $media->media_url_https = $object->getMediaUrlHttps();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getMediaUrlHttps() does only exist in the following implementations of said interface: Twitter\Object\AbstractTwitterMedia, Twitter\Object\TwitterExtendedEntity, Twitter\Object\TwitterMedia.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
49 3
        $media->url = $object->getUrl();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getUrl() does only exist in the following implementations of said interface: Twitter\Object\AbstractTwitterMedia, Twitter\Object\TwitterExtendedEntity, Twitter\Object\TwitterMedia, Twitter\Object\TwitterUrl, Twitter\Object\TwitterVariantMedia.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
50 3
        $media->display_url = $object->getDisplayUrl();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getDisplayUrl() does only exist in the following implementations of said interface: Twitter\Object\AbstractTwitterMedia, Twitter\Object\TwitterExtendedEntity, Twitter\Object\TwitterMedia, Twitter\Object\TwitterUrl.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
51 3
        $media->expanded_url = $object->getExpandedUrl();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getExpandedUrl() does only exist in the following implementations of said interface: Twitter\Object\AbstractTwitterMedia, Twitter\Object\TwitterExtendedEntity, Twitter\Object\TwitterMedia, Twitter\Object\TwitterUrl.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
52 3
        $media->type = $object->getType();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getType() does only exist in the following implementations of said interface: Twitter\Object\AbstractTwitterMedia, Twitter\Object\TwitterCoordinates, Twitter\Object\TwitterDelete, Twitter\Object\TwitterEvent, Twitter\Object\TwitterExtendedEntity, Twitter\Object\TwitterMedia.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
53 3
        $media->indices = $this->entityIndicesSerializer->serialize($object->getIndices());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getIndices() does only exist in the following implementations of said interface: Twitter\Object\AbstractTwitterMedia, Twitter\Object\TwitterExtendedEntity, Twitter\Object\TwitterHashtag, Twitter\Object\TwitterMedia, Twitter\Object\TwitterSymbol, Twitter\Object\TwitterUrl, Twitter\Object\TwitterUserMention, Twitter\TwitterEntity.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
54
55 3
        $media->sizes = [];
56 3
        foreach ($object->getSizes() as $size) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getSizes() does only exist in the following implementations of said interface: Twitter\Object\AbstractTwitterMedia, Twitter\Object\TwitterExtendedEntity, Twitter\Object\TwitterMedia.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
57 3
            $media->sizes[$size->getName()] = $this->mediaSizeSerializer->serialize($size);
58 2
        }
59
60 3
        return $media;
61
    }
62
63
    /**
64
     * @param  \stdClass $obj
65
     * @param  array     $context
66
     * @return TwitterMedia
67
     */
68 3
    public function unserialize($obj, array $context = [])
69
    {
70 3
        if (!$this->canUnserialize($obj)) {
71 3
            throw new \InvalidArgumentException('$object is not unserializable');
72 3
        }
73 3
74 2
        $sizesObjects = [];
75 3
        if ($obj->sizes) {
76 2
            foreach ($obj->sizes as $sizeName => $sizeObj) {
77 2
                $sizesObjects[$sizeName] = $this->mediaSizeSerializer->unserialize(
78 2
                    $sizeObj,
79
                    [TwitterMediaSizeSerializer::NAME_VAR => $sizeName]
80 3
                );
81 3
            }
82 3
        }
83 3
84 3
        return TwitterMedia::create(
85 3
            $obj->id,
86 3
            $obj->media_url,
87 2
            $obj->media_url_https,
88 3
            $obj->url,
89 3
            $obj->display_url,
90 2
            $obj->expanded_url,
91
            $sizesObjects,
92
            $obj->type,
93
            $this->entityIndicesSerializer->unserialize($obj->indices)
94
        );
95
    }
96 21
97
    /**
98 21
     * @param  TwitterSerializable $object
99 21
     * @return boolean
100 21
     */
101 14
    public function canSerialize(TwitterSerializable $object)
102
    {
103
        return $object instanceof TwitterMedia;
104
    }
105
106
    /**
107
     * @param  \stdClass $object
108
     * @return boolean
109
     */
110
    public function canUnserialize($object)
111
    {
112
        return isset($object->media_url) && !isset($object->duration_millis);
113
    }
114
115
    /**
116
     * @return TwitterMediaSerializer
117
     */
118
    public static function build()
119
    {
120
        return new self(
121
            TwitterEntityIndicesSerializer::build(),
122
            TwitterMediaSizeSerializer::build()
123
        );
124
    }
125
}
126