Failed Conditions
Push — v7 ( 3784e0...1ab069 )
by Florent
01:37
created

CompressionMethodsManager::list()   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\Encryption\Compression;
15
16
/**
17
 * Compression method manager.
18
 */
19
final class CompressionMethodsManager
20
{
21
    /**
22
     * @var CompressionInterface[]
23
     */
24
    private $compressionMethods = [];
25
26
    /**
27
     * @param CompressionInterface[] $methods
28
     *
29
     * @return CompressionMethodsManager
30
     */
31
    public static function create(array $methods): CompressionMethodsManager
32
    {
33
        $manager = new self();
34
        foreach ($methods as $method) {
35
            $manager->add($method);
36
        }
37
38
        return $manager;
39
    }
40
41
    /**
42
     * @param CompressionInterface $compressionMethod
43
     */
44
    public function add(CompressionInterface $compressionMethod)
45
    {
46
        $this->compressionMethods[$compressionMethod->name()] = $compressionMethod;
47
    }
48
49
    /**
50
     * @param string $name
51
     *
52
     * @return bool
53
     */
54
    public function has(string $name): bool
55
    {
56
        return array_key_exists($name, $this->compressionMethods);
57
    }
58
59
    /**
60
     * This method will try to find a CompressionInterface object able to support the compression method.
61
     *
62
     * @param string $name The name of the compression method
63
     *
64
     * @return CompressionInterface
65
     */
66
    public function get(string $name): CompressionInterface
67
    {
68
        if (!$this->has($name)) {
69
            throw new \InvalidArgumentException(sprintf('The compression method "%s" is not supported.', $name));
70
        }
71
72
        return $this->compressionMethods[$name];
73
    }
74
75
    /**
76
     * @return string[]
77
     */
78
    public function list(): array
79
    {
80
        return array_keys($this->compressionMethods);
81
    }
82
}
83