Completed
Push — master ( 9bc3b7...a15851 )
by Harry
02:46
created

ConvertEncoding::canModify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Graze\DataFile\Modify;
4
5
use Graze\DataFile\Helper\GetOptionTrait;
6
use Graze\DataFile\Helper\OptionalLoggerTrait;
7
use Graze\DataFile\Helper\Process\ProcessFactoryAwareInterface;
8
use Graze\DataFile\Node\FileNodeInterface;
9
use Graze\DataFile\Node\LocalFile;
10
use InvalidArgumentException;
11
use Psr\Log\LoggerAwareInterface;
12
use Psr\Log\LogLevel;
13
use Symfony\Component\Process\Exception\ProcessFailedException;
14
15
/**
16
 * Convert the Encoding of a file
17
 *
18
 * For a list of the supported encodings run:
19
 *
20
 * ```bash
21
 * iconv -l
22
 * ```
23
 */
24
class ConvertEncoding implements FileModifierInterface, LoggerAwareInterface, ProcessFactoryAwareInterface
25
{
26
    use OptionalLoggerTrait;
27
    use FileProcessTrait;
28
    use GetOptionTrait;
29
30
    /**
31
     * @extend Graze\DataFile\Node\File\LocalFile Only apply to local files
32
     *
33
     * @param LocalFile $file
34
     * @param string    $encoding             Encoding as defined by iconv
35
     * @param array     $options              -postfix <string> (Default: toEncoding)
36
     *                                        -keepOldFile <bool> (Default: true)
37
     *
38
     * @return LocalFile
39
     */
40 7
    public function toEncoding(LocalFile $file, $encoding, array $options = [])
41
    {
42 7
        $this->options = $options;
43
44 7
        $pathInfo = pathinfo($file->getPath());
45
46 7
        $outputFileName = sprintf(
47 7
            '%s-%s.%s',
48 7
            $pathInfo['filename'],
49 7
            $this->getOption('postfix', $encoding),
50 7
            $pathInfo['extension']
51
        );
52
53 7
        $output = $file->getClone()
54 7
                       ->setPath($pathInfo['dirname'] . '/' . $outputFileName)
55 7
                       ->setEncoding($encoding);
56
57
        $cmd = "iconv " .
58 7
            ($file->getEncoding() ? "--from-code={$file->getEncoding()} " : '') .
59 7
            "--to-code={$encoding} " .
60 7
            "{$file->getPath()} " .
61 7
            "> {$output->getPath()}";
62
63 7
        $this->log(LogLevel::INFO, "Converting file: '{file}' encoding to '{encoding}'", [
64 7
            'file'     => $file,
65 7
            'encoding' => $encoding,
66
        ]);
67
68 7
        return $this->processFile($file, $output, $cmd, $this->getOption('keepOldFile', true));
69
    }
70
71
    /**
72
     * Can this file be modified by this modifier
73
     *
74
     * @param FileNodeInterface $file
75
     *
76
     * @return bool
77
     */
78 3
    public function canModify(FileNodeInterface $file)
79
    {
80
        return (
81 3
            ($file instanceof localFile) &&
82 3
            ($file->exists())
83
        );
84
    }
85
86
    /**
87
     * Modify the file
88
     *
89
     * @param FileNodeInterface $file
90
     * @param array             $options List of options:
91
     *                                   -encoding <string>
92
     *                                   -postfix <string> (Default: toEncoding) Set this to blank to replace inline
93
     *                                   -keepOldFile <bool> (Default: true)
94
     *
95
     * @return FileNodeInterface
96
     */
97 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...
98
    {
99 2
        if (!$this->canModify($file) || !($file instanceof LocalFile)) {
100 1
            throw new InvalidArgumentException("Supplied: $file is not a valid LocalFile");
101
        }
102
103 1
        $this->options = $options;
104 1
        $encoding = $this->requireOption('encoding');
105 1
        unset($options['encoding']);
106
107 1
        return $this->toEncoding($file, $encoding, $options);
108
    }
109
}
110