Completed
Pull Request — master (#3)
by Harry
03:17
created

LocalFile::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Node;
15
16
use Graze\DataFile\Modify\Compress\CompressionFactory;
17
use GuzzleHttp\Psr7\LazyOpenStream;
18
use GuzzleHttp\Psr7\Stream;
19
use League\Flysystem\Adapter\Local;
20
use League\Flysystem\Filesystem;
21
use Psr\Http\Message\StreamInterface;
22
23
class LocalFile extends FileNode implements LocalFileNodeInterface, NodeStreamInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $compression;
29
30
    /**
31
     * @var string|null
32
     */
33
    protected $encoding;
34
35
    /**
36
     * @param string $path
37
     */
38 109
    public function __construct($path)
39
    {
40 109
        parent::__construct(new FileSystem(new Local('/')), $path);
41
42 109
        $this->compression = CompressionFactory::TYPE_NONE;
43 109
        $this->encoding = null;
44 109
    }
45
46
    /**
47
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|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 getEncoding()
50
    {
51 14
        return $this->encoding;
52
    }
53
54
    /**
55
     * @param string $encoding
56
     *
57
     * @return $this
58
     */
59 15
    public function setEncoding($encoding)
60
    {
61 15
        $this->encoding = $encoding;
62
63 15
        return $this;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 20
    public function getContents()
70
    {
71 20
        if ($this->exists() &&
72 19
            $this->getCompression() != CompressionFactory::TYPE_NONE
73 20
        ) {
74 2
            $factory = new CompressionFactory();
75 2
            $compressor = $factory->getDeCompressor($this->getCompression());
76 2
            $uncompressed = $compressor->decompress($this);
77 2
            $content = $uncompressed->getContents();
78 2
            $uncompressed->delete();
79 2
            return $content;
80
        } else {
81 20
            return parent::getContents();
82
        }
83
    }
84
85
    /**
86
     * @return string - see CompressionFactory
87
     */
88 31
    public function getCompression()
89
    {
90 31
        return $this->compression;
91
    }
92
93
    /**
94
     * @param string $compression - @see CompressionFactory
95
     *
96
     * @return $this
97
     */
98 22
    public function setCompression($compression)
99
    {
100 22
        $this->compression = $compression;
101
102 22
        return $this;
103
    }
104
105
    /**
106
     * Get a stream for the given node
107
     *
108
     * @param string $mode
109
     *
110
     * @return StreamInterface
111
     */
112 1
    public function getStream($mode = 'c+')
113
    {
114 1
        return new LazyOpenStream($this->getPath(), $mode);
115
    }
116
}
117