Export::unlock()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\Console\Output;
11
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Filesystem\Exception\IOException;
14
15
/**
16
 * Export output to file.
17
 *
18
 * @author  Peter Gribanov <[email protected]>
19
 */
20
class Export extends Decorator
21
{
22
    /**
23
     * File handle.
24
     *
25
     * @var resource
26
     */
27
    protected $handle;
28
29
    /**
30
     * Append to end file.
31
     *
32
     * @var bool
33
     */
34
    protected $append = true;
35
36
    /**
37
     * @param OutputInterface $output
38
     * @param string $filename
39
     * @param bool $append
40
     */
41 75
    public function __construct(OutputInterface $output, $filename, $append = true)
42
    {
43 75
        parent::__construct($output);
44 75
        $this->append = $append;
45
46 75
        $dir = pathinfo($filename, PATHINFO_DIRNAME);
47 75
        if (!is_dir($dir) && true !== @mkdir($dir, 0755, true)) {
48 1
            throw new IOException('Failed to create the export directory: '.$dir);
49
        }
50 74
        if (!($this->handle = @fopen($filename, 'w'))) {
51 1
            throw new IOException('Failed to open the export file: '.$filename);
52
        }
53 73
    }
54
55
    /**
56
     * @param array|string $messages
57
     * @param bool|false $newline
58
     * @param int $type
59
     */
60 49
    public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
61
    {
62 49
        $this->writeToFile($messages, $newline);
63 49
        parent::write($messages, $newline, $type);
64 49
    }
65
66
    /**
67
     * @param array|string $messages
68
     * @param int $type
69
     */
70 24
    public function writeln($messages, $type = self::OUTPUT_NORMAL)
71
    {
72 24
        $this->writeToFile($messages, true);
73 24
        parent::writeln($messages, $type);
74 24
    }
75
76
    /**
77
     * @param string|array $messages
78
     * @param bool $newline
79
     */
80 73
    protected function writeToFile($messages, $newline)
81
    {
82 73
        $messages = (array) $messages;
83 73
        foreach ($messages as $key => $message) {
84 73
            $messages[$key] = strip_tags($message).($newline ? PHP_EOL : '');
85 73
        }
86
87
        // reset file
88 73
        if (!$this->append) {
89 37
            rewind($this->handle);
90 37
            ftruncate($this->handle, 0);
91 37
        }
92
93 73
        fwrite($this->handle, implode('', $messages));
94 73
    }
95
96 73
    public function unlock()
97
    {
98 73
        if (is_resource($this->handle)) {
99 73
            fclose($this->handle);
100 73
        }
101 73
    }
102
103 73
    public function __destruct()
104
    {
105 73
        $this->unlock();
106 73
    }
107
}
108