Completed
Push — master ( d87b02...4573bb )
by Harry
04:40
created

DeCompressorTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 81
ccs 15
cts 15
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B decompress() 0 24 3
log() 0 1 ?
processFile() 0 6 ?
getExtension() 0 1 ?
getName() 0 1 ?
getDecompressCommand() 0 1 ?
1
<?php
2
3
namespace Graze\DataFile\Modify\Compress;
4
5
use Graze\DataFile\Node\LocalFileNodeInterface;
6
use InvalidArgumentException;
7
use Psr\Log\LogLevel;
8
9
trait DeCompressorTrait
10
{
11
    /**
12
     * Decompress a file and return the decompressed file
13
     *
14
     * @param LocalFileNodeInterface $node
15
     * @param array                  $options
16
     *
17
     * @return LocalFileNodeInterface
18
     */
19 12
    public function decompress(LocalFileNodeInterface $node, array $options = [])
20
    {
21 12
        $pathInfo = pathinfo($node->getPath());
22
23 12
        if (!$node->exists()) {
24 2
            throw new InvalidArgumentException("The file: $node does not exist");
25
        }
26
27 10
        $outputFile = $node->getClone()
28 10
                           ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'])
29 10
                           ->setCompression(CompressionFactory::TYPE_NONE);
30
31 10
        $this->log(LogLevel::INFO, "DeCompressing file: {file} into {target} using {compression}", [
32 10
            'file'        => $node,
33 10
            'target'      => $outputFile,
34 10
            'compression' => $this->getName(),
35 10
        ]);
36
37 10
        $cmd = $this->getDecompressCommand($node, $outputFile);
38
39 10
        $keepOld = (isset($options['keepOldFile'])) ? $options['keepOldFile'] : true;
40
41 10
        return $this->processFile($node, $outputFile, $cmd, $keepOld);
42
    }
43
44
    /**
45
     * Abstract Log function that might should be handed by the OptionalLoggerTrait or similar
46
     *
47
     * @param string $level
48
     * @param string $message
49
     * @param array  $context
50
     *
51
     * @return void
52
     */
53
    abstract protected function log($level, $message, array $context = []);
54
55
    /**
56
     * @param LocalFileNodeInterface $node
57
     * @param LocalFileNodeInterface $outputFile
58
     * @param string                 $cmd
59
     * @param bool                   $keepOld
60
     *
61
     * @return LocalFileNodeInterface
62
     */
63
    abstract protected function processFile(
64
        LocalFileNodeInterface $node,
65
        LocalFileNodeInterface $outputFile,
66
        $cmd,
67
        $keepOld = true
68
    );
69
70
    /**
71
     * @return string
72
     */
73
    abstract public function getExtension();
74
75
    /**
76
     * @return string
77
     */
78
    abstract public function getName();
79
80
    /**
81
     * Get the command line to decompress a file
82
     *
83
     * @param LocalFileNodeInterface $from
84
     * @param LocalFileNodeInterface $to
85
     *
86
     * @return string
87
     */
88
    abstract public function getDecompressCommand(LocalFileNodeInterface $from, LocalFileNodeInterface $to);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $to. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
89
}
90