Completed
Push — master ( d87b02...4573bb )
by Harry
04:40
created

CompressionFactory::isCompression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Graze\DataFile\Modify\Compress;
4
5
use Graze\DataFile\Modify\Exception\InvalidCompressionTypeException;
6
7
class CompressionFactory
8
{
9
    const TYPE_NONE    = 'none';
10
    const TYPE_UNKNOWN = 'unknown';
11
12
    /**
13
     * @var CompressionTypeInterface[]
14
     */
15
    private $compressors;
16
17
    /**
18
     * @var CompressionTypeInterface[]
19
     */
20
    private $deCompressors;
21
22 14
    public function __construct()
23
    {
24
        // build known compression types
25 14
        $gzip = new Gzip();
26 14
        $zip = new Zip();
27
28 14
        $this->addCompressor($gzip);
29 14
        $this->addDecompressor($gzip);
30 14
        $this->addCompressor($zip);
31 14
        $this->addDecompressor($zip);
32 14
    }
33
34
    /**
35
     * @param CompressionTypeInterface $type
36
     *
37
     * @return static
0 ignored issues
show
Documentation introduced by
Should the return type not be CompressionFactory|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
38
     */
39 14
    public function addCompressor(CompressionTypeInterface $type)
40
    {
41 14
        $this->compressors[$type->getName()] = $type;
42 14
    }
43
44
    /**
45
     * @param CompressionTypeInterface $type
46
     *
47
     * @return static
0 ignored issues
show
Documentation introduced by
Should the return type not be CompressionFactory|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
48
     */
49 14
    public function addDeCompressor(CompressionTypeInterface $type)
50
    {
51 14
        $this->deCompressors[$type->getName()] = $type;
52 14
    }
53
54
    /**
55
     * Check if the specified $compression is valid or not
56
     *
57
     * @param string $compression
58
     *
59
     * @return bool
60
     */
61 3
    public function isCompression($compression)
62
    {
63 3
        return isset($this->compressors[$compression]);
64
    }
65
66
    /**
67
     * @param string $compression
68
     *
69
     * @return CompressorInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be CompressionTypeInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
70
     * @throws InvalidCompressionTypeException
71
     */
72 5
    public function getCompressor($compression)
73
    {
74 5
        if (isset($this->compressors[$compression])) {
75 2
            return $this->compressors[$compression];
76
        } else {
77 3
            throw new InvalidCompressionTypeException($compression);
78
        }
79
    }
80
81
    /**
82
     * @param string $compression CompressionType::
83
     *
84
     * @return DeCompressorInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be CompressionTypeInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
85
     * @throws InvalidCompressionTypeException
86
     */
87 7
    public function getDeCompressor($compression)
88
    {
89 7
        if (isset($this->deCompressors[$compression])) {
90 4
            return $this->deCompressors[$compression];
91
        } else {
92 3
            throw new InvalidCompressionTypeException($compression);
93
        }
94
    }
95
}
96