Failed Conditions
Push — v7 ( 3de5c8...73ebba )
by Florent
01:54
created

AlgorithmManagerFactory::aliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\Core;
15
16
/**
17
 * Class AlgorithmManagerFactory
18
 */
19
final class AlgorithmManagerFactory
20
{
21
    /**
22
     * @var array
23
     */
24
    private $algorithms = [];
25
26
    /**
27
     * @param string       $alias
28
     * @param AlgorithmInterface $algorithm
29
     *
30
     * @return AlgorithmManagerFactory
31
     */
32
    public function add(string $alias, AlgorithmInterface $algorithm): AlgorithmManagerFactory
33
    {
34
        if (array_key_exists($alias, $this->algorithms)) {
35
            throw new \InvalidArgumentException(sprintf('The alias "%s" already exists.', $alias));
36
        }
37
        $this->algorithms[$alias] = $algorithm;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @param string[] $aliases
44
     *
45
     * @return AlgorithmManager
46
     */
47
    public function create(array $aliases): AlgorithmManager
48
    {
49
        $algorithms = [];
50
        foreach ($aliases as $alias) {
51
            if (array_key_exists($alias, $this->algorithms)) {
52
                $algorithms[] = $this->algorithms[$alias];
53
            } else {
54
                throw new \InvalidArgumentException(sprintf('The algorithm with the alias "%s" is not supported.', $alias));
55
            }
56
        }
57
58
        return AlgorithmManager::create($algorithms);
59
    }
60
61
    /**
62
     * @return string[]
63
     */
64
    public function aliases(): array
65
    {
66
        return array_keys($this->algorithms);
67
    }
68
}
69