Passed
Push — preview-1.19.0 ( 1cfa57...b8cce4 )
by Guilherme
06:39
created

UpdateSentVerificationSubscriber::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 3
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PROCERGS\LoginCidadao\PhoneVerificationBundle\Event;
12
13
use Eljam\CircuitBreaker\Breaker;
14
use LoginCidadao\PhoneVerificationBundle\Event\UpdateStatusEvent;
15
use LoginCidadao\PhoneVerificationBundle\Exception\InvalidSentVerificationStatusException;
16
use LoginCidadao\PhoneVerificationBundle\PhoneVerificationEvents;
17
use PROCERGS\Sms\Exception\SmsExceptionInterface;
18
use PROCERGS\Sms\Exception\TransactionNotFoundException;
19
use PROCERGS\Sms\Protocols\SmsInterface;
20
use PROCERGS\Sms\Protocols\SmsStatusInterface;
21
use PROCERGS\Sms\SmsService;
22
use Psr\Log\LoggerAwareInterface;
23
use Psr\Log\LoggerAwareTrait;
24
use Psr\Log\LoggerTrait;
25
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
26
27
class UpdateSentVerificationSubscriber implements EventSubscriberInterface, LoggerAwareInterface
28
{
29
    use LoggerAwareTrait, LoggerTrait;
30
31
    /** @var SmsService */
32
    private $smsService;
33
34
    /** @var Breaker */
35
    private $breaker;
36
37
    /**
38
     * PhoneVerificationSubscriber constructor.
39
     *
40
     * @param SmsService $smsService
41
     * @param Breaker $breaker
42
     */
43 3
    public function __construct(SmsService $smsService, Breaker $breaker)
44
    {
45 3
        $this->smsService = $smsService;
46 3
        $this->breaker = $breaker;
47 3
    }
48
49
    /**
50
     * Logs with an arbitrary level.
51
     *
52
     * @param mixed $level
53
     * @param string $message
54
     * @param array $context
55
     *
56
     * @return void
57
     */
58 1
    protected function log($level, $message, array $context = array())
59
    {
60 1
        if ($this->logger) {
61 1
            $this->logger->log($level, $message, $context);
62
        }
63 1
    }
64
65
    /**
66
     * Returns an array of event names this subscriber wants to listen to.
67
     *
68
     * The array keys are event names and the value can be:
69
     *
70
     *  * The method name to call (priority defaults to 0)
71
     *  * An array composed of the method name to call and the priority
72
     *  * An array of arrays composed of the method names to call and respective
73
     *    priorities, or 0 if unset
74
     *
75
     * For instance:
76
     *
77
     *  * array('eventName' => 'methodName')
78
     *  * array('eventName' => array('methodName', $priority))
79
     *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
80
     *
81
     * @return array The event names to listen to
82
     */
83 1
    public static function getSubscribedEvents()
84
    {
85
        return [
86 1
            PhoneVerificationEvents::PHONE_VERIFICATION_GET_SENT_VERIFICATION_STATUS => 'onStatusRequested',
87
        ];
88
    }
89
90
    /**
91
     * @param UpdateStatusEvent $event
92
     * @throws \Exception
93
     */
94 3
    public function onStatusRequested(UpdateStatusEvent $event)
95
    {
96 3
        $transactionId = $event->getTransactionId();
97
98
        try {
99 3
            $sms = $this->protectedGetStatus($this->smsService, $transactionId);
100 1
            $status = $sms->getStatus();
101 1
            if (!$status instanceof SmsStatusInterface) {
102
                throw new InvalidSentVerificationStatusException(
103 1
                    "No status available for transaction {$transactionId}"
104
                );
105
            }
106 2
        } catch (TransactionNotFoundException $e) {
107 1
            return;
108 1
        } catch (InvalidSentVerificationStatusException|SmsExceptionInterface $e) {
109 1
            throw new InvalidSentVerificationStatusException(
110 1
                "Error for transaction id {$transactionId}: {$e->getMessage()}",
111 1
                $e->getCode(),
112 1
                $e
113
            );
114
        }
115
116 1
        $event->setDeliveryStatus($status);
0 ignored issues
show
Bug introduced by
$status of type PROCERGS\Sms\Protocols\SmsStatusInterface is incompatible with the type LoginCidadao\PhoneVerifi...odel\SmsStatusInterface expected by parameter $deliveryStatus of LoginCidadao\PhoneVerifi...nt::setDeliveryStatus(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
        $event->setDeliveryStatus(/** @scrutinizer ignore-type */ $status);
Loading history...
117 1
        unset($sms);
118 1
    }
119
120
    /**
121
     * @param SmsService $smsService
122
     * @param $transactionId
123
     * @return SmsInterface
124
     * @throws \Exception|TransactionNotFoundException
125
     */
126
    private function protectedGetStatus(SmsService $smsService, $transactionId)
127
    {
128 3
        return $this->breaker->protect(function () use ($smsService, $transactionId) {
0 ignored issues
show
Unused Code introduced by
The import $smsService is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
129 3
            $statuses = $this->smsService->getStatus($transactionId);
130
131 1
            $this->info(
132 1
                'Updating status for transaction ID: {transaction_id}',
133 1
                ['transaction_id' => $transactionId]
134
            );
135
136 1
            return $statuses;
137 3
        });
138
    }
139
}
140