CompressedFormat::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 9
cp 0.7778
rs 9.8333
c 0
b 0
f 0
cc 4
nc 3
nop 3
crap 4.1755
1
<?php
2
3
namespace Mathielen\ImportEngine\Storage\Format;
4
5
class CompressedFormat extends Format
6
{
7
    /**
8
     * @var Format
9
     */
10
    private $subFormat;
11
12
    private $uriInsideArchive;
13
    private $wrapper;
14
15
    protected $name = 'Compressed File';
16
    protected $id = 'compress';
17
18 3
    public function __construct($uriInsideArchive = null, $wrapper = 'zip', Format $subFormat = null)
19
    {
20 3
        if (!is_string($wrapper)) {
21
            throw new \InvalidArgumentException('wrapper argument must be a string');
22
        }
23 3
        if (!is_null($uriInsideArchive) && !is_string($uriInsideArchive)) {
24
            throw new \InvalidArgumentException('uriInsideArchive argument must be a string');
25
        }
26
27 3
        $this->uriInsideArchive = $uriInsideArchive;
28 3
        $this->wrapper = $wrapper;
29 3
        $this->subFormat = $subFormat;
30 3
    }
31
32 1
    public function getSubFormat()
33
    {
34 1
        return $this->subFormat;
35
    }
36
37 1
    public function getInsideStream(\SplFileInfo $file)
38
    {
39 1
        if (is_null($this->uriInsideArchive)) {
40
            throw new \LogicException('This compressed archive has multiple files in it. Cannot create a single stream.');
41
        }
42
43 1
        $streamUri = $this->wrapper.'://'.$file.'#'.$this->uriInsideArchive;
44 1
        $uncompressedUri = tempnam('/tmp', 'compressed');
45 1
        file_put_contents($uncompressedUri, file_get_contents($streamUri));
46
47 1
        return new \SplFileInfo($uncompressedUri);
48
    }
49
50
    public function __toString()
51
    {
52
        return $this->name.' with sub-format: '.$this->subFormat;
53
    }
54
}
55