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

SmsAdapterProvider::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 12
rs 10
c 1
b 0
f 0
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 Surfnet\StepupGateway\ApiBundle\Exception\InvalidArgumentException;
22
use function array_key_exists;
23
use function get_class;
24
use function implode;
25
use function in_array;
26
use function sprintf;
27
28
class SmsAdapterProvider
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SmsAdapterProvider
Loading history...
29
{
30
    private const SPRYNG = 'spryng';
31
    private const MESSAGEBIRD = 'messagebird';
32
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
33
     * @var SmsAdapterInterface[]
34
     */
35
    private $services;
0 ignored issues
show
Coding Style introduced by
Private member variable "services" must be prefixed with an underscore
Loading history...
36
37
    private static $allowedServices = [
0 ignored issues
show
Coding Style introduced by
Private member variable "allowedServices" must be prefixed with an underscore
Loading history...
38
        SpryngService::class => self::SPRYNG,
39
        MessageBirdService::class => self::MESSAGEBIRD,
40
    ];
41
42
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
43
     * @var string
44
     */
45
    private $selectedService;
0 ignored issues
show
Coding Style introduced by
Private member variable "selectedService" must be prefixed with an underscore
Loading history...
46
47
    public function __construct(string $selectedService)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
48
    {
49
        if (!in_array($selectedService, self::$allowedServices)) {
50
            throw new InvalidArgumentException(
51
                sprintf(
52
                    'The selected SMS service (%s) is not supported, choose one of: %s',
53
                    $selectedService,
54
                    implode(', ', self::$allowedServices)
55
                )
56
            );
57
        }
58
        $this->selectedService = $selectedService;
59
    }
60
61
    public function addSmsAdapter(SmsAdapterInterface $adapter)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addSmsAdapter()
Loading history...
62
    {
63
        $adapterName = get_class($adapter);
64
        if (!array_key_exists($adapterName, self::$allowedServices)) {
65
            throw new InvalidArgumentException(
66
                sprintf(
67
                    'Unable to add this adapter, this implementation (%s) is not supported',
68
                    $adapterName
69
                )
70
            );
71
        }
72
        $this->services[self::$allowedServices[$adapterName]] = $adapter;
73
    }
74
75
    public function getSelectedService(): SmsAdapterInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getSelectedService()
Loading history...
76
    {
77
        return $this->services[$this->selectedService];
78
    }
79
}
80