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

TwitterDirectMessageSerializer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 3
Metric Value
wmc 10
c 6
b 0
f 3
lcom 1
cbo 4
dl 0
loc 99
ccs 29
cts 29
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A serialize() 0 19 3
A unserialize() 0 16 3
A canSerialize() 0 4 1
A canUnserialize() 0 4 1
A build() 0 7 1
1
<?php
2
3
namespace Twitter\Serializer;
4
5
use Twitter\Object\TwitterDate;
6
use Twitter\Object\TwitterDirectMessage;
7
use Twitter\TwitterMessageId;
8
use Twitter\TwitterSerializable;
9
use Twitter\TwitterSerializer;
10
11
class TwitterDirectMessageSerializer implements TwitterSerializer
12
{
13
    /**
14
     * @var TwitterUserSerializer
15
     */
16
    private $userSerializer;
17
18
    /**
19
     * @var TwitterEntitiesSerializer
20
     */
21
    private $twitterEntitiesSerializer;
22
23
    /**
24
     * @param TwitterUserSerializer $userSerializer
25
     * @param TwitterEntitiesSerializer $twitterEntitiesSerializer
26
     */
27 15
    public function __construct(
28
        TwitterUserSerializer $userSerializer,
29
        TwitterEntitiesSerializer $twitterEntitiesSerializer
30
    ) {
31 15
        $this->userSerializer = $userSerializer;
32 15
        $this->twitterEntitiesSerializer = $twitterEntitiesSerializer;
33 15
    }
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 TwitterDirectMessage');
43
        }
44
45 3
        $dm = new \stdClass();
46 3
        $dm->id = (string)$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
        $dm->sender = $this->userSerializer->serialize($object->getSender());
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 getSender() does only exist in the following implementations of said interface: Twitter\Object\AbstractMessage, Twitter\Object\Tweet, Twitter\Object\TwitterDirectMessage.

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
        $dm->recipient = $this->userSerializer->serialize($object->getRecipient());
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 getRecipient() does only exist in the following implementations of said interface: Twitter\Object\TwitterDirectMessage.

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
        $dm->text = $object->getText();
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 getText() does only exist in the following implementations of said interface: Twitter\Object\AbstractMessage, Twitter\Object\Tweet, Twitter\Object\TwitterDirectMessage, Twitter\Object\TwitterHashtag, Twitter\Object\TwitterSymbol.

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
        $dm->created_at = $object->getDate()->setTimezone(new \DateTimeZone('UTC'))->format(TwitterDate::FORMAT);
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 getDate() does only exist in the following implementations of said interface: Twitter\Object\AbstractMessage, Twitter\Object\Tweet, Twitter\Object\TwitterDelete, Twitter\Object\TwitterDirectMessage, Twitter\Object\TwitterEvent.

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
        $dm->entities = $object->getEntities()?$this->twitterEntitiesSerializer->serialize($object->getEntities()):null;
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 getEntities() does only exist in the following implementations of said interface: Twitter\Object\AbstractMessage, Twitter\Object\Tweet, Twitter\Object\TwitterDirectMessage.

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
53 3
        $dmObject = new \stdClass();
54
        $dmObject->direct_message = $dm;
55
56
        return $dmObject;
57
    }
58
59
    /**
60
     * @param  \stdClass $directMessage
61 3
     * @param  array     $context
62
     * @return TwitterDirectMessage
63 3
     */
64 3
    public function unserialize($directMessage, array $context = [])
65 3
    {
66 3
        if (!$this->canUnserialize($directMessage)) {
67 3
            throw new \InvalidArgumentException('$object is not unserializable');
68 3
        }
69 3
70 2
        $dm = $directMessage->direct_message;
71
        return TwitterDirectMessage::create(
72
            TwitterMessageId::create($dm->id),
73
            $this->userSerializer->unserialize($dm->sender),
74
            $this->userSerializer->unserialize($dm->recipient),
75
            $dm->text,
76 6
            new \DateTimeImmutable($dm->created_at),
77
            $dm->entities?$this->twitterEntitiesSerializer->unserialize($dm->entities):null
78 6
        );
79 6
    }
80 6
81 4
    /**
82
     * @param  TwitterSerializable $object
83
     * @return boolean
84
     */
85
    public function canSerialize(TwitterSerializable $object)
86
    {
87
        return $object instanceof TwitterDirectMessage;
88
    }
89
90
    /**
91
     * @param  \stdClass $object
92
     * @return boolean
93
     */
94
    public function canUnserialize($object)
95
    {
96
        return (isset($object->direct_message));
97
    }
98
99
    /**
100
     * @return TwitterDirectMessageSerializer
101
     */
102
    public static function build()
103
    {
104
        return new self(
105
            TwitterUserSerializer::build(),
106
            TwitterEntitiesSerializer::build()
107
        );
108
    }
109
}
110