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

ConvertEncoding::toEncoding()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 2

Importance

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