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