Passed
Pull Request — develop (#295)
by Peter
04:30
created

SpryngService   A

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
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SpryngService
Loading history...
28
{
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @var LoggerInterface
31
     */
32
    private $logger;
0 ignored issues
show
Coding Style introduced by
Private member variable "logger" must be prefixed with an underscore
Loading history...
33
34
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
35
     * @var MessageClient
36
     */
37
    private $client;
0 ignored issues
show
Coding Style introduced by
Private member variable "client" must be prefixed with an underscore
Loading history...
38
39
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
40
     * @var string
41
     */
42
    private $route = null;
0 ignored issues
show
Coding Style introduced by
Private member variable "route" must be prefixed with an underscore
Loading history...
43
44
    public function __construct(string $apiKey, ?string $route, LoggerInterface $logger)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function send()
Loading history...
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