Passed
Pull Request — master (#20)
by Hilmi Erdem
10:06
created

FormatManager::get()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 4
nop 1
1
<?php
2
/*
3
 * Copyright (c) 2021. Hilmi Erdem Keren
4
 * license MIT
5
 */
6
7
namespace Erdemkeren\Otp;
8
9
use Erdemkeren\Otp\Contracts\FormatContract;
10
use Erdemkeren\Otp\Exceptions\UnknownOtpFormat;
11
use Erdemkeren\Otp\Contracts\FormatManagerContract;
12
13
class FormatManager implements FormatManagerContract
14
{
15
    private static array $formats;
16
17
    public function __construct(private string $defaultFormat)
18
    {
19
    }
20
21
    public function get(string $name): FormatContract
22
    {
23
        $name = $name === 'default' ? $this->defaultFormat : $name;
24
        if (! array_key_exists($name, static::$formats)) {
0 ignored issues
show
Bug introduced by
Since $formats is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $formats to at least protected.
Loading history...
25
            throw UnknownOtpFormat::createForName($name);
26
        }
27
28
        return static::$formats[$name];
29
    }
30
31
    public function register(FormatContract $format): void
32
    {
33
        static::$formats[$format->name()] = $format;
0 ignored issues
show
Bug introduced by
Since $formats is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $formats to at least protected.
Loading history...
34
    }
35
}
36