Completed
Push — master ( 7ad838...d2ad41 )
by Florent
16:56
created

CompressionMethodManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 87
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 9 2
A add() 0 9 2
A has() 0 4 1
A get() 0 8 2
A list() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 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
class CompressionMethodManager
17
{
18
    /**
19
     * @var CompressionMethod[]
20
     */
21
    private $compressionMethods = [];
22
23
    /**
24
     * CompressionMethodManager constructor.
25
     *
26
     * @deprecated Will be private in v2.x. Please use CompressionManager::create() instead.
27
     */
28
    public function __construct()
29
    {
30
    }
31
32
    /**
33
     * This method creates a Compression Manager with the selected compression methods.
34
     *
35
     * @param CompressionMethod[] $methods
36
     *
37
     * @return CompressionMethodManager
38
     */
39
    public static function create(array $methods): self
40
    {
41
        $manager = new self();
42
        foreach ($methods as $method) {
43
            $manager->add($method);
44
        }
45
46
        return $manager;
47
    }
48
49
    /**
50
     * Add the given compression method to the manager.
51
     *
52
     * @param CompressionMethod $compressionMethod
53
     */
54
    protected function add(CompressionMethod $compressionMethod)
55
    {
56
        $name = $compressionMethod->name();
57
        if ($this->has($name)) {
58
            throw new \InvalidArgumentException(sprintf('The compression method "%s" is already supported.', $name));
59
        }
60
61
        $this->compressionMethods[$name] = $compressionMethod;
62
    }
63
64
    /**
65
     * Returns true if the givn compression method is supported.
66
     *
67
     * @param string $name
68
     *
69
     * @return bool
70
     */
71
    public function has(string $name): bool
72
    {
73
        return array_key_exists($name, $this->compressionMethods);
74
    }
75
76
    /**
77
     * This method returns the compression method with the given name.
78
     * Throws an exception if the method is not supported.
79
     *
80
     * @param string $name The name of the compression method
81
     *
82
     * @return CompressionMethod
83
     */
84
    public function get(string $name): CompressionMethod
85
    {
86
        if (!$this->has($name)) {
87
            throw new \InvalidArgumentException(sprintf('The compression method "%s" is not supported.', $name));
88
        }
89
90
        return $this->compressionMethods[$name];
91
    }
92
93
    /**
94
     * Returns the list of compression method names supported by the manager.
95
     *
96
     * @return string[]
97
     */
98
    public function list(): array
99
    {
100
        return array_keys($this->compressionMethods);
101
    }
102
}
103