Completed
Pull Request — master (#13)
by Harry
03:21
created

LocalFile::getCompression()   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 0
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 League\Flysystem\Adapter\Local;
19
use League\Flysystem\Filesystem;
20
use Psr\Http\Message\StreamInterface;
21
22
class LocalFile extends FileNode implements LocalFileNodeInterface, NodeStreamInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $compression;
28
29
    /**
30
     * @var string|null
31
     */
32
    protected $encoding;
33
34
    /**
35
     * @param string $path
36
     */
37 108
    public function __construct($path)
38
    {
39 108
        parent::__construct(new FileSystem(new Local('/')), $path);
40
41 108
        $this->compression = CompressionFactory::TYPE_NONE;
42 108
        $this->encoding = null;
43 108
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 20
    public function getContents()
49
    {
50 20
        if ($this->exists() &&
51 20
            $this->getCompression() != CompressionFactory::TYPE_NONE
52
        ) {
53 2
            $factory = new CompressionFactory();
54 2
            $compressor = $factory->getDeCompressor($this->getCompression());
55 2
            $uncompressed = $compressor->decompress($this);
56 2
            $content = $uncompressed->getContents();
57 2
            $uncompressed->delete();
58 2
            return $content;
59
        } else {
60 20
            return parent::getContents();
61
        }
62
    }
63
64
    /**
65
     * Get a stream for the given node
66
     *
67
     * @param string $mode
68
     *
69
     * @return StreamInterface
70
     */
71 2
    public function getStream($mode = 'c+')
72
    {
73 2
        return new LazyOpenStream($this->getPath(), $mode);
74
    }
75
}
76