SmsAdapterProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 48
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSelectedService() 0 3 1
A __construct() 0 12 2
A addSmsAdapter() 0 12 2
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 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
29
{
30
    private const SPRYNG = 'spryng';
31
    /**
32
     * @var SmsAdapterInterface[]
33
     */
34
    private $services;
35
36
    private static $allowedServices = [
37
        SpryngService::class => self::SPRYNG,
38
    ];
39
40
    /**
41
     * @var string
42
     */
43
    private $selectedService;
44
45
    public function __construct(string $selectedService)
46
    {
47
        if (!in_array($selectedService, self::$allowedServices)) {
48
            throw new InvalidArgumentException(
49
                sprintf(
50
                    'The selected SMS service (%s) is not supported, choose one of: %s',
51
                    $selectedService,
52
                    implode(', ', self::$allowedServices)
53
                )
54
            );
55
        }
56
        $this->selectedService = $selectedService;
57
    }
58
59
    public function addSmsAdapter(SmsAdapterInterface $adapter): void
60
    {
61
        $adapterName = get_class($adapter);
62
        if (!array_key_exists($adapterName, self::$allowedServices)) {
63
            throw new InvalidArgumentException(
64
                sprintf(
65
                    'Unable to add this adapter, this implementation (%s) is not supported',
66
                    $adapterName
67
                )
68
            );
69
        }
70
        $this->services[self::$allowedServices[$adapterName]] = $adapter;
71
    }
72
73
    public function getSelectedService(): SmsAdapterInterface
74
    {
75
        return $this->services[$this->selectedService];
76
    }
77
}
78