SpryngService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 18 4
A __construct() 0 5 1
1
<?php
2
3
/**
4
 * Copyright 2021 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\ApiBundle\Sms;
20
21
use Psr\Log\LoggerInterface;
22
use Spryng\SpryngRestApi\Objects\Message;
23
use Spryng\SpryngRestApi\Resources\MessageClient;
24
use Spryng\SpryngRestApi\Spryng;
25
use Surfnet\StepupGateway\ApiBundle\Dto\SmsMessage;
26
27
class SpryngService implements SmsAdapterInterface
28
{
29
    /**
30
     * @var LoggerInterface
31
     */
32
    private $logger;
33
34
    /**
35
     * @var MessageClient
36
     */
37
    private $client;
38
39
    /**
40
     * @var string
41
     */
42
    private $route = null;
43
44
    public function __construct(string $apiKey, ?string $route, LoggerInterface $logger)
45
    {
46
        $this->client = new MessageClient(new Spryng($apiKey));
47
        $this->route = $route;
48
        $this->logger = $logger;
49
    }
50
51
    public function send(SmsMessage $message): SmsMessageResultInterface
52
    {
53
        $this->logger->notice('Using Spryng to send an SMS');
54
55
        $spryngMessage = new Message();
56
        $spryngMessage->setBody($message->body);
57
        $spryngMessage->setRecipients([$message->recipient]);
58
        $spryngMessage->setOriginator($message->originator);
59
60
        if (!is_null($this->route)) {
0 ignored issues
show
introduced by
The condition is_null($this->route) is always false.
Loading history...
61
            $spryngMessage->setRoute($this->route);
62
        }
63
64
        $response = $this->client->create($spryngMessage);
65
        if (is_null($response) || !$response->wasSuccessful()) {
66
            $this->logger->warning('Sending OTP per SMS failed.');
67
        }
68
        return new SpryngMessageResult($response);
69
    }
70
}
71