Completed
Push — master ( 1a19ee...d67a52 )
by Harry
8s
created

ConvertEncoding::modify()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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