Failed Conditions
Push — v7 ( 0b17f0...b3d8c9 )
by Florent
02:08
created

AlgorithmManagerFactory::all()   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
     * @return string[]
44
     */
45
    public function aliases(): array
46
    {
47
        return array_keys($this->algorithms);
48
    }
49
50
    /**
51
     * @return AlgorithmInterface[]
52
     */
53
    public function all(): array
54
    {
55
        return $this->algorithms;
56
    }
57
58
    /**
59
     * @param string[] $aliases
60
     *
61
     * @return AlgorithmManager
62
     */
63
    public function create(array $aliases): AlgorithmManager
64
    {
65
        $algorithms = [];
66
        foreach ($aliases as $alias) {
67
            if (array_key_exists($alias, $this->algorithms)) {
68
                $algorithms[] = $this->algorithms[$alias];
69
            } else {
70
                throw new \InvalidArgumentException(sprintf('The algorithm with the alias "%s" is not supported.', $alias));
71
            }
72
        }
73
74
        return AlgorithmManager::create($algorithms);
75
    }
76
}
77