Completed
Push — wip-platform ( 03cba6...2999ab )
by
unknown
05:53 queued 02:49
created

BaseWriter::open()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 *
5
 * Copyright (C) 2015-2017 Libre Informatique
6
 *
7
 * This file is licenced under the GNU LGPL v3.
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Blast\Bundle\CoreBundle\Admin;
13
14
use Exporter\Writer\WriterInterface;
15
16
abstract class BaseWriter implements WriterInterface
17
{
18
    protected $filename;
19
    protected $file;
20
    protected $position;
21
22
    public function configure($filename)
23
    {
24
        $this->filename = $filename;
25
        $this->position = 0;
26
27
        if (is_file($filename)) {
28
            throw new \RuntimeException(sprintf('The file %s already exist', $filename));
29
        }
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function open()
36
    {
37
        $this->file = fopen($this->filename, 'w', false);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function close()
44
    {
45
        fclose($this->file);
46
    }
47
48
    protected function _write($data, $separator = "\n")
49
    {
50
        fwrite($this->file, ($this->position > 0 ? $separator : '') . $data);
51
        ++$this->position;
52
    }
53
}
54