ConvertEncodingTransformer::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
namespace TreeHouse\Feeder\Resource\Transformer;
4
5
use TreeHouse\Feeder\Resource\FileResource;
6
use TreeHouse\Feeder\Resource\ResourceCollection;
7
use TreeHouse\Feeder\Resource\ResourceInterface;
8
use TreeHouse\Feeder\Transport\FileTransport;
9
10
/**
11
 * Converts character encoding.
12
 */
13
class ConvertEncodingTransformer implements ResourceTransformerInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $fromEncoding;
19
20
    /**
21
     * @var string
22
     */
23
    protected $toEncoding;
24
25
    /**
26
     * @param string $fromEncoding The encoding in which the resource is initially
27
     * @param string $toEncoding   The encoding to convert to. Uses internal encoding when left empty
28
     */
29 8
    public function __construct($fromEncoding, $toEncoding = null)
30
    {
31 8
        $this->fromEncoding = $fromEncoding;
32 8
        $this->toEncoding = $toEncoding ?: mb_internal_encoding();
33 8
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38 6
    public function transform(ResourceInterface $resource, ResourceCollection $collection)
39
    {
40 6
        $file = $resource->getFile()->getPathname();
41
42
        // first, rename the original file
43 6
        $oldFile = $this->rename($file);
44
45 6
        $old = fopen($oldFile, 'r');
46 6
        $new = fopen($file, 'w');
47 6
        while (!feof($old)) {
48 6
            fwrite($new, mb_convert_encoding(fgets($old), $this->toEncoding, $this->fromEncoding));
49 6
        }
50
51 6
        fclose($old);
52 6
        fclose($new);
53
54 6
        unlink($oldFile);
55
56 6
        $transport = FileTransport::create($file);
57
58 6
        if ($resource->getTransport()) {
59
            $transport->setDestinationDir($resource->getTransport()->getDestinationDir());
60
        }
61
62 6
        return new FileResource($transport);
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68 2
    public function needsTransforming(ResourceInterface $resource)
69
    {
70 2
        $file = $resource->getFile();
71 2
        while (!$file->eof()) {
72 2
            $line = $file->fgets();
73 2
            if (!mb_check_encoding($line, $this->toEncoding)) {
74
                return true;
75
            }
76 2
        }
77
78 2
        return false;
79
    }
80
81
    /**
82
     * @param string $file
83
     *
84
     * @throws \RuntimeException
85
     *
86
     * @return string
87
     */
88 6
    protected function rename($file)
89
    {
90 6
        $tmpFile = $file . '.tmp';
91
92 6
        if (rename($file, $tmpFile)) {
93 6
            return $tmpFile;
94
        }
95
96
        throw new \RuntimeException(sprintf('Could not rename %s to %s', $file, $tmpFile));
97
    }
98
}
99