Completed
Push — preview-1.19.0 ( 740511...1cfa57 )
by Guilherme
06:02 queued 01:09
created

onStatusRequested()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5.3374

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 10
nop 1
dl 0
loc 30
ccs 16
cts 21
cp 0.7619
crap 5.3374
rs 8.439
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\InvalidStatusException;
18
use PROCERGS\Sms\Exception\SmsExceptionInterface;
19
use PROCERGS\Sms\Exception\TransactionNotFoundException;
20
use PROCERGS\Sms\Protocols\SmsInterface;
21
use PROCERGS\Sms\Protocols\SmsStatusInterface;
22
use PROCERGS\Sms\SmsService;
23
use Psr\Log\LoggerAwareInterface;
24
use Psr\Log\LoggerAwareTrait;
25
use Psr\Log\LoggerTrait;
26
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
27
28
class UpdateSentVerificationSubscriber implements EventSubscriberInterface, LoggerAwareInterface
29
{
30
    use LoggerAwareTrait, LoggerTrait;
31
32
    /** @var SmsService */
33
    private $smsService;
34
35
    /** @var Breaker */
36
    private $breaker;
37
38
    /**
39
     * PhoneVerificationSubscriber constructor.
40
     *
41
     * @param SmsService $smsService
42
     * @param Breaker $breaker
43
     */
44 3
    public function __construct(
45
        SmsService $smsService,
46
        Breaker $breaker
47
    ) {
48 3
        $this->smsService = $smsService;
49 3
        $this->breaker = $breaker;
50 3
    }
51
52
    /**
53
     * Logs with an arbitrary level.
54
     *
55
     * @param mixed $level
56
     * @param string $message
57
     * @param array $context
58
     *
59
     * @return void
60
     */
61 1
    protected function log($level, $message, array $context = array())
62
    {
63 1
        if ($this->logger) {
64 1
            $this->logger->log($level, $message, $context);
65
        }
66 1
    }
67
68
    /**
69
     * Returns an array of event names this subscriber wants to listen to.
70
     *
71
     * The array keys are event names and the value can be:
72
     *
73
     *  * The method name to call (priority defaults to 0)
74
     *  * An array composed of the method name to call and the priority
75
     *  * An array of arrays composed of the method names to call and respective
76
     *    priorities, or 0 if unset
77
     *
78
     * For instance:
79
     *
80
     *  * array('eventName' => 'methodName')
81
     *  * array('eventName' => array('methodName', $priority))
82
     *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
83
     *
84
     * @return array The event names to listen to
85
     */
86 1
    public static function getSubscribedEvents()
87
    {
88
        return [
89 1
            PhoneVerificationEvents::PHONE_VERIFICATION_GET_SENT_VERIFICATION_STATUS => 'onStatusRequested',
90
        ];
91
    }
92
93
    /**
94
     * @param UpdateStatusEvent $event
95
     * @throws \Exception
96
     */
97 3
    public function onStatusRequested(UpdateStatusEvent $event)
98
    {
99 3
        $transactionId = $event->getTransactionId();
100
101
        try {
102 3
            $sms = $this->protectedGetStatus($this->smsService, $transactionId);
103 1
            $status = $sms->getStatus();
104 1
            if (!$status instanceof SmsStatusInterface) {
105
                throw new InvalidSentVerificationStatusException(
106 1
                    "No status available for transaction {$transactionId}"
107
                );
108
            }
109 2
        } catch (TransactionNotFoundException $e) {
110 1
            return;
111 1
        } catch (SmsExceptionInterface $e) {
112
            throw new InvalidSentVerificationStatusException(
113
                "Error for transaction id {$transactionId}: {$e->getMessage()}",
114
                $e->getCode(),
115
                $e
116
            );
117 1
        } catch (InvalidSentVerificationStatusException $e) {
118 1
            throw new InvalidSentVerificationStatusException(
119 1
                "Error for transaction id {$transactionId}: {$e->getMessage()}",
120 1
                $e->getCode(),
121 1
                $e
122
            );
123
        }
124
125 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

125
        $event->setDeliveryStatus(/** @scrutinizer ignore-type */ $status);
Loading history...
126 1
        unset($sms);
127 1
    }
128
129
    /**
130
     * @param SmsService $smsService
131
     * @param $transactionId
132
     * @return SmsInterface
133
     * @throws \Exception|TransactionNotFoundException
134
     */
135 3
    private function protectedGetStatus(SmsService $smsService, $transactionId)
136
    {
137 3
        return $this->breaker->protect(
138 3
            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...
139 3
                $statuses = $this->smsService->getStatus($transactionId);
140
141 1
                $this->info(
142 1
                    'Updating status for transaction ID: {transaction_id}',
143 1
                    ['transaction_id' => $transactionId]
144
                );
145
146 1
                return $statuses;
147 3
            }
148
        );
149
    }
150
}
151