Failed Conditions
Push — v7 ( 2109ab...348606 )
by Florent
02:23
created

CompressionMethodManagerFactory::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
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\Encryption\Compression;
15
16
final class CompressionMethodManagerFactory
17
{
18
    /**
19
     * @var CompressionMethodInterface[]
20
     */
21
    private $compressionMethods = [];
22
23
    /**
24
     * @param string                     $alias
25
     * @param CompressionMethodInterface $compressionMethod
26
     *
27
     * @return CompressionMethodManagerFactory
28
     */
29
    public function add(string $alias, CompressionMethodInterface $compressionMethod): CompressionMethodManagerFactory
30
    {
31
        if (array_key_exists($alias, $this->compressionMethods)) {
32
            throw new \InvalidArgumentException(sprintf('The alias "%s" already exists.', $alias));
33
        }
34
        $this->compressionMethods[$alias] = $compressionMethod;
35
36
        return $this;
37
    }
38
39
    /**
40
     * @param string[] $aliases
41
     *
42
     * @return CompressionMethodManager
43
     */
44
    public function create(array $aliases): CompressionMethodManager
45
    {
46
        $compressionMethods = [];
47
        foreach ($aliases as $alias) {
48
            if (array_key_exists($alias, $this->compressionMethods)) {
49
                $compressionMethods[] = $this->compressionMethods[$alias];
50
            } else {
51
                throw new \InvalidArgumentException(sprintf('The compression method with the alias "%s" is not supported.', $alias));
52
            }
53
        }
54
55
        return CompressionMethodManager::create($compressionMethods);
56
    }
57
}
58