Completed
Pull Request — master (#1)
by Harry
04:42
created

FindCompression::canModify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Graze\DataFile\Modify\Compress;
4
5
use Graze\DataFile\Helper\OptionalLoggerTrait;
6
use Graze\DataFile\Helper\Process\ProcessFactoryAwareInterface;
7
use Graze\DataFile\Helper\Process\ProcessTrait;
8
use Graze\DataFile\Modify\FileModifierInterface;
9
use Graze\DataFile\Node\FileNodeInterface;
10
use Graze\DataFile\Node\LocalFileNodeInterface;
11
use InvalidArgumentException;
12
use Psr\Log\LoggerAwareInterface;
13
use Psr\Log\LogLevel;
14
15
class FindCompression implements ProcessFactoryAwareInterface, LoggerAwareInterface, FileModifierInterface
16
{
17
    use ProcessTrait;
18
    use OptionalLoggerTrait;
19
20
    /**
21
     * @var CompressionFactory
22
     */
23
    private $factory;
24
25
    /**
26
     * FindCompression constructor.
27
     *
28
     * @param CompressionFactory $factory
29
     */
30 8
    public function __construct(CompressionFactory $factory)
31
    {
32 8
        $this->factory = $factory;
33 8
    }
34
35
    /**
36
     * Find the compression of a file
37
     *
38
     * @param LocalFileNodeInterface $file
39
     *
40
     * @return string
41
     */
42 6
    public function getCompression(LocalFileNodeInterface $file)
43
    {
44 6
        $cmd = "file --brief --uncompress --mime {$file->getPath()}";
45
46 6
        $process = $this->getProcess($cmd);
47 6
        $process->mustRun();
48
49 5
        $result = $process->getOutput();
50 5
        if (preg_match('/compressed-encoding=application\/(?:x-)?(.+?);/i', $result, $matches)) {
51 4
            if ($this->factory->isCompression($matches[1])) {
52 3
                $this->log(LogLevel::DEBUG, "Found the compression for '{file}' as '{compression}'", [
53 3
                    'file'        => $file,
54 3
                    'compression' => $matches[1],
55
                ]);
56 3
                return $matches[1];
57
            }
58 1
            return CompressionFactory::TYPE_UNKNOWN;
59
        }
60 1
        return CompressionFactory::TYPE_NONE;
61
    }
62
63
    /**
64
     * Can this file be modified by this modifier
65
     *
66
     * @param FileNodeInterface $file
67
     *
68
     * @return bool
69
     */
70 3
    public function canModify(FileNodeInterface $file)
71
    {
72 3
        return ($file instanceof LocalFileNodeInterface &&
73 3
            $file->exists());
74
    }
75
76
    /**
77
     * Modify the file
78
     *
79
     * @param FileNodeInterface $file
80
     * @param array             $options
81
     *
82
     * @return FileNodeInterface
83
     */
84 2 View Code Duplication
    public function modify(FileNodeInterface $file, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86 2
        if (!$this->canModify($file) || !($file instanceof LocalFileNodeInterface)) {
87 1
            throw new InvalidArgumentException("The supplied file: '$file' does not implement LocalFileNodeInterface'");
88
        }
89
90 1
        $file->setCompression($this->getCompression($file));
91
92 1
        return $file;
93
    }
94
}
95