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

TwitterEventSerializer::canUnserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Twitter\Serializer;
4
5
use Twitter\Object\TwitterDate;
6
use Twitter\Object\TwitterEvent;
7
use Twitter\TwitterSerializable;
8
use Twitter\TwitterSerializer;
9
10
class TwitterEventSerializer implements TwitterSerializer
11
{
12
    /**
13
     * @var TwitterUserSerializer
14
     */
15
    private $userSerializer;
16
17
    /**
18
     * @var TwitterEventTargetSerializer
19
     */
20
    private $targetSerializer;
21
22
    /**
23
     * @param TwitterUserSerializer $userSerializer
24
     * @param TwitterEventTargetSerializer $targetSerializer
25
     */
26 15
    public function __construct(TwitterUserSerializer $userSerializer, TwitterEventTargetSerializer $targetSerializer)
27
    {
28 15
        $this->userSerializer = $userSerializer;
29 15
        $this->targetSerializer = $targetSerializer;
30 15
    }
31
32
    /**
33
     * @param  TwitterSerializable $object
34
     * @return \stdClass
35
     */
36 6
    public function serialize(TwitterSerializable $object)
37
    {
38 6
        if (!$this->canSerialize($object)) {
39 3
            throw new \InvalidArgumentException('$object must be an instance of TwitterEvent');
40
        }
41
42 3
        $event = new \stdClass();
43 3
        $event->event = $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...
44 3
        $event->source = $this->userSerializer->serialize($object->getSource());
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 getSource() does only exist in the following implementations of said interface: Twitter\Object\Tweet, 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...
45 3
        $event->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...
46
47 3
        if ($object->getTarget()) {
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 getTarget() does only exist in the following implementations of said interface: 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...
48 3
            $event->target = $this->userSerializer->serialize($object->getTarget());
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 getTarget() does only exist in the following implementations of said interface: 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...
49 2
        }
50
51 3
        if ($object->getObject()) {
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 getObject() does only exist in the following implementations of said interface: 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...
52 3
            $event->target_object = $this->targetSerializer->serialize($object->getObject());
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 getObject() does only exist in the following implementations of said interface: 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...
53 2
        }
54
55 3
        return $event;
56
    }
57
58
    /**
59
     * @param  \stdClass $obj
60
     * @param  array     $context
61
     * @return TwitterEvent
62
     */
63 3
    public function unserialize($obj, array $context = [])
64
    {
65 3
        if (!$this->canUnserialize($obj)) {
66 3
            throw new \InvalidArgumentException('$object is not unserializable');
67 3
        }
68 3
69 3
        return TwitterEvent::create(
70 3
            $obj->event,
71 2
            $this->userSerializer->unserialize($obj->source),
72
            isset($obj->target) ? $this->userSerializer->unserialize($obj->target) : null,
73
            isset($obj->target_object) ? $this->targetSerializer->unserialize($obj->target_object) : null,
74
            new \DateTimeImmutable($obj->created_at)
75
        );
76
    }
77 6
78
    /**
79 6
     * @param  TwitterSerializable $object
80 6
     * @return boolean
81 6
     */
82 4
    public function canSerialize(TwitterSerializable $object)
83
    {
84
        return $object instanceof TwitterEvent;
85
    }
86
87
    /**
88
     * @param  \stdClass $object
89
     * @return boolean
90
     */
91
    public function canUnserialize($object)
92
    {
93
        return isset($object->event);
94
    }
95
96
    /**
97
     * @return TwitterEventSerializer
98
     */
99
    public static function build()
100
    {
101
        return new self(
102
            TwitterUserSerializer::build(),
103
            TwitterEventTargetSerializer::build()
104
        );
105
    }
106
}
107