Completed
Push — master ( 10e391...d5f2dc )
by Harry
06:06
created

LocalFile   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 5
c 5
b 0
f 1
lcom 0
cbo 7
dl 0
loc 54
rs 10
ccs 16
cts 16
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getContents() 0 15 3
A getStream() 0 4 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
    public function __construct($path)
38 108
    {
39
        parent::__construct(new FileSystem(new Local('/')), $path);
40 108
41
        $this->compression = CompressionFactory::TYPE_NONE;
42 108
        $this->encoding = null;
43 108
    }
44 108
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getContents()
49 14
    {
50
        if ($this->exists() &&
51 14
            $this->getCompression() != CompressionFactory::TYPE_NONE
52
        ) {
53
            $factory = new CompressionFactory();
54
            $compressor = $factory->getDeCompressor($this->getCompression());
55
            $uncompressed = $compressor->decompress($this);
56
            $content = $uncompressed->getContents();
57
            $uncompressed->delete();
58
            return $content;
59 15
        } else {
60
            return parent::getContents();
61 15
        }
62
    }
63 15
64
    /**
65
     * Get a stream for the given node
66
     *
67
     * @param string $mode
68
     *
69 20
     * @return StreamInterface
70
     */
71 20
    public function getStream($mode = 'c+')
72 19
    {
73 20
        return new LazyOpenStream($this->getPath(), $mode);
74 2
    }
75
}
76