Completed
Push — v2.0.x ( 2b2b8e...21aa40 )
by Florent
03:32
created

CompressionManagerFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Factory;
13
14
use Jose\Compression\CompressionInterface;
15
use Jose\Compression\CompressionManager;
16
17
final class CompressionManagerFactory
18
{
19
    /**
20
     * @param array $methods
21
     *
22
     * @return \Jose\Compression\CompressionManagerInterface
23
     */
24
    public static function createCompressionManager(array $methods)
25
    {
26
        $compression_manager = new CompressionManager();
27
28
        foreach ($methods as $key => $value) {
29
            if ($value instanceof CompressionInterface) {
30
                $compression_manager->addCompressionAlgorithm($value);
31
            } elseif (is_string($value)) {
32
                $class = self::getMethodClass($value);
33
                $compression_manager->addCompressionAlgorithm(new $class());
34
            } else {
35
                $class = self::getMethodClass($key);
36
                $compression_manager->addCompressionAlgorithm(new $class($value));
37
            }
38
        }
39
40
        return $compression_manager;
41
    }
42
43
    /**
44
     * @param string $method
45
     *
46
     * @return bool
47
     */
48
    private static function isAlgorithmSupported($method)
49
    {
50
        return array_key_exists($method, self::getSupportedMethods());
51
    }
52
53
    /**
54
     * @param string $method
55
     *
56
     * @throws \InvalidArgumentException
57
     *
58
     * @return string
59
     */
60
    private static function getMethodClass($method)
61
    {
62
        if (self::isAlgorithmSupported($method)) {
63
            return self::getSupportedMethods()[$method];
64
        }
65
        throw new \InvalidArgumentException(sprintf('Compression method "%s" is not supported.', $method));
66
    }
67
68
    private static function getSupportedMethods()
69
    {
70
        return [
71
            'DEF'  => '\Jose\Compression\Deflate',
72
            'GZ'   => '\Jose\Compression\GZip',
73
            'ZLIB' => '\Jose\Compression\ZLib',
74
        ];
75
    }
76
}
77