Completed
Push — master ( c5f2a4...b3cdb4 )
by Edgar
04:10
created

FileOutput::defineContentType()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 7
nc 4
nop 2
crap 3
1
<?php
2
namespace nstdio\svg\output;
3
4
use Gaufrette\Adapter\Local;
5
use Gaufrette\Filesystem;
6
7
/**
8
 * Class FileOutput
9
 *
10
 * @package nstdio\svg\output
11
 * @author  Edgar Asatryan <[email protected]>
12
 */
13
class FileOutput implements FileOutputInterface
14
{
15
    /**
16
     * @var Local
17
     */
18
    private $adapter;
19
20
    /**
21
     * @var Filesystem
22
     */
23
    private $filesystem;
24
25
    /**
26
     * @inheritdoc
27
     */
28 2
    public function file($name, $content, $override)
29
    {
30 2
        $this->lazyInit($name);
31
32 2
        $this->defineContentType($name, $content);
33
34 2
        return $this->filesystem->write($name, $content, $override);
35
    }
36
37 2
    private function lazyInit($fileName)
38
    {
39 2
        if ($this->shouldInit()) {
40 2
            $this->adapter = new Local(dirname($fileName), true);
41 2
            $this->filesystem = new Filesystem($this->adapter);
42 2
        }
43 2
    }
44
45
    /**
46
     * @return bool
47
     */
48 2
    private function shouldInit()
49
    {
50 2
        return $this->adapter === null && $this->filesystem === null;
51
    }
52
53 2
    private function defineContentType(&$name, &$content)
54
    {
55 2
        $info = pathinfo($name);
56 2
        if (isset($info['extension']) === false) {
57 1
            $info['extension'] = 'svg';
58 1
        }
59
60 2
        if (strtolower($info['extension']) === 'svgz') {
61 1
            $content = gzencode($content, 9);
62 1
        }
63
64 2
        $name = $info['filename'] . "." . $info['extension'];
65
    }
66
}