Completed
Pull Request — master (#1)
by Harry
03:40
created

FindEncoding::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\Encoding;
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
use Symfony\Component\Process\Exception\ProcessFailedException;
15
16
class FindEncoding implements ProcessFactoryAwareInterface, LoggerAwareInterface, FileModifierInterface
17
{
18
    use ProcessTrait;
19
    use OptionalLoggerTrait;
20
21
    /**
22
     * Find the Encoding of a specified file
23
     *
24
     * @param LocalFileNodeInterface $file
25
     *
26
     * @return null|string
27
     * @throws ProcessFailedException
28
     */
29 6
    public function getEncoding(LocalFileNodeInterface $file)
30
    {
31 6
        $cmd = "file --brief --uncompress --mime {$file->getPath()}";
32
33 6
        $process = $this->getProcess($cmd);
34 6
        $process->mustRun();
35
36 5
        $result = $process->getOutput();
37 5
        if (preg_match('/charset=([^\s]+)/i', $result, $matches)) {
38 4
            $this->log(LogLevel::DEBUG, "Found the encoding for '{file}' as '{encoding}'", [
39 4
                'file'     => $file,
40 4
                'encoding' => $matches[1],
41
            ]);
42 4
            return $matches[1];
43
        }
44 1
        return null;
45
    }
46
47
    /**
48
     * Can this file be modified by this modifier
49
     *
50
     * @param FileNodeInterface $file
51
     *
52
     * @return bool
53
     */
54 3
    public function canModify(FileNodeInterface $file)
55
    {
56 3
        return ($file instanceof LocalFileNodeInterface &&
57 3
            $file->exists());
58
    }
59
60
    /**
61
     * Modify the file
62
     *
63
     * @param FileNodeInterface $file
64
     * @param array             $options
65
     *
66
     * @return FileNodeInterface
67
     * @throws InvalidArgumentException
68
     */
69 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...
70
    {
71 2
        if (!$this->canModify($file) || !($file instanceof LocalFileNodeInterface)) {
72 1
            throw new InvalidArgumentException(
73 1
                "The specified file: '$file' does not implement LocalFileNodeInterface'"
74
            );
75
        }
76
77 1
        $file->setEncoding($this->getEncoding($file));
78
79 1
        return $file;
80
    }
81
}
82