GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MPNS::createPayload()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 15
nc 4
nop 0
1
<?php
2
3
/*
4
 * (c) Alexander Zhukov <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Zbox\UnifiedPush\Notification\PayloadHandler;
11
12
use Zbox\UnifiedPush\Message\MessageInterface;
13
use Zbox\UnifiedPush\Notification\PayloadHandler;
14
use Zbox\UnifiedPush\Message\Type\MPNSBase as MPNSMessage;
15
use Zbox\UnifiedPush\Message\Type\MPNSRaw;
16
17
/**
18
 * Class MPNS
19
 * @package Zbox\UnifiedPush\Notification\PayloadHandler
20
 */
21
class MPNS extends PayloadHandler
22
{
23
    /**
24
     * The maximum size allowed for MPNS message payload is 3K bytes
25
     */
26
    const PAYLOAD_MAX_LENGTH = 3072;
27
28
    /**
29
     * Notification delivery interval
30
     *
31
     * @var int
32
     */
33
    protected $delayInterval;
34
35
    /**
36
     * @param MessageInterface $message
37
     * @return bool
38
     */
39
    public function isSupported(MessageInterface $message)
40
    {
41
        return $message instanceof MPNSMessage;
42
    }
43
44
    /**
45
     * @return \DOMDocument
46
     */
47
    public function createPayload()
48
    {
49
        /** @var MPNSMessage $message */
50
        $message = $this->message;
51
52
        $messageType = ucfirst($message->getMPNSType());
53
54
        $document      = new \DOMDocument("1.0", "utf-8");
55
        $baseElement  = $document->createElement("wp:Notification");
56
        $baseElement->setAttribute("xmlns:wp", "WPNotification");
57
        $document->appendChild($baseElement);
58
59
        $elementName = $message instanceof MPNSRaw ? 'root' : "wp:" . $messageType;
60
        $rootElement = $document->createElement($elementName);
61
        $baseElement->appendChild($rootElement);
62
63
        if ($message instanceof MPNSRaw) {
64
            $this->completeRawDocumentBody($document, $rootElement, $message);
65
        } else {
66
            $this->completeDocumentBody($document, $rootElement, $message);
67
        }
68
69
        return $document;
70
    }
71
72
    /**
73
     * @param \DOMDocument $payload
74
     * @return string
75
     */
76
    public function packPayload($payload)
77
    {
78
        return $payload->saveXML();
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    public function getCustomNotificationData()
85
    {
86
        return
87
            array(
88
                'message_id'        => $this->notificationId,
89
                'delay_interval'    => $this->message->getDelayInterval(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zbox\UnifiedPush\Message\MessageInterface as the method getDelayInterval() does only exist in the following implementations of said interface: Zbox\UnifiedPush\Message\Type\MPNSBase, Zbox\UnifiedPush\Message\Type\MPNSRaw, Zbox\UnifiedPush\Message\Type\MPNSTile, Zbox\UnifiedPush\Message\Type\MPNSToast.

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...
90
                'message_type'      => $this->message->getMPNSType()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zbox\UnifiedPush\Message\MessageInterface as the method getMPNSType() does only exist in the following implementations of said interface: Zbox\UnifiedPush\Message\Type\MPNSBase, Zbox\UnifiedPush\Message\Type\MPNSRaw, Zbox\UnifiedPush\Message\Type\MPNSTile, Zbox\UnifiedPush\Message\Type\MPNSToast.

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...
91
            );
92
    }
93
94
    /**
95
     * @param \DOMDocument $document
96
     * @param \DOMElement $rootElement
97
     * @param MPNSMessage $message
98
     */
99
    protected function completeDocumentBody(
100
        \DOMDocument $document,
101
        \DOMElement $rootElement,
102
        MPNSMessage $message
103
    ) {
104
        foreach ($message->getPropertiesList() as $property)
105
        {
106
            $propertyName   = ucfirst($property->getName());
107
            $getterName     = 'get' . $propertyName;
108
            $value          = $this->$getterName();
109
110
            if ($value) {
111
                $name    = "wp:" . $propertyName;
112
                $element = $document->createElement($name, $value);
113
                $rootElement->appendChild($element);
114
            }
115
        }
116
    }
117
118
    /**
119
     * @param \DOMDocument $document
120
     * @param \DOMElement $rootElement
121
     * @param MPNSRaw $message
122
     */
123
    protected function completeRawDocumentBody(
124
        \DOMDocument $document,
125
        \DOMElement $rootElement,
126
        MPNSRaw $message
127
    ) {
128
        foreach ($message->getRawPayload() as $key => $value) {
129
            $element = $document->createElement($key, $value);
130
            $rootElement->appendChild($element);
131
        }
132
    }
133
}
134