Listener::onVerificationFailure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Mdb\PayPal\Ipn;
4
5
use Mdb\PayPal\Ipn\Event\MessageInvalidEvent;
6
use Mdb\PayPal\Ipn\Event\MessageVerificationFailureEvent;
7
use Mdb\PayPal\Ipn\Event\MessageVerifiedEvent;
8
use Mdb\PayPal\Ipn\Exception\ServiceException;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
11
class Listener
12
{
13
    public const IPN_VERIFIED_EVENT = 'ipn.message.verified';
14
    public const IPN_INVALID_EVENT = 'ipn.message.invalid';
15
    public const IPN_VERIFICATION_FAILURE_EVENT = 'ipn.message.verification_failure';
16
17
    /**
18
     * @var MessageFactory
19
     */
20
    private $messageFactory;
21
22
    /**
23
     * @var Verifier
24
     */
25
    private $verifier;
26
27
    /**
28
     * @var EventDispatcherInterface
29
     */
30
    private $eventDispatcher;
31
32
    public function __construct(
33
        MessageFactory $messageFactory,
34
        Verifier $verifier,
35
        EventDispatcherInterface $eventDispatcher
36
    ) {
37
        $this->messageFactory = $messageFactory;
38
        $this->verifier = $verifier;
39
        $this->eventDispatcher = $eventDispatcher;
40
    }
41
42
    public function listen() : void
43
    {
44
        $message = $this->messageFactory->createMessage();
45
46
        try {
47
            $result = $this->verifier->verify($message);
48
49
            if ($result) {
50
                $eventName = self::IPN_VERIFIED_EVENT;
51
                $event = new MessageVerifiedEvent($message);
52
            } else {
53
                $eventName = self::IPN_INVALID_EVENT;
54
                $event = new MessageInvalidEvent($message);
55
            }
56
        } catch (\UnexpectedValueException | ServiceException $e) {
57
            $eventName = self::IPN_VERIFICATION_FAILURE_EVENT;
58
            $event = new MessageVerificationFailureEvent($message, $e->getMessage());
59
        }
60
61
        $this->eventDispatcher->dispatch($event, $eventName);
0 ignored issues
show
Documentation introduced by
$event is of type object<Mdb\PayPal\Ipn\Ev...t\MessageVerifiedEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
    }
63
64
    public function onVerified(callable $listener) : void
65
    {
66
        $this->eventDispatcher->addListener(self::IPN_VERIFIED_EVENT, $listener);
67
    }
68
69
    public function onInvalid(callable $listener) : void
70
    {
71
        $this->eventDispatcher->addListener(self::IPN_INVALID_EVENT, $listener);
72
    }
73
74
    public function onVerificationFailure(callable $listener) : void
75
    {
76
        $this->eventDispatcher->addListener(self::IPN_VERIFICATION_FAILURE_EVENT, $listener);
77
    }
78
}
79