Passed
Push — master ( 0c1223...63e4c8 )
by Fabrice
02:56
created

FileLoaderAbstract::writeBom()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of YaEtl.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\YaEtl\Loaders\File;
11
12
use fab2s\NodalFlow\NodalFlowException;
13
use fab2s\NodalFlow\YaEtlException;
14
use fab2s\YaEtl\Loaders\LoaderAbstract;
15
use fab2s\YaEtl\Traits\FileHandlerTrait;
16
17
/**
18
 * Class FileLoaderAbstract
19
 */
20
abstract class FileLoaderAbstract extends LoaderAbstract
21
{
22
    use FileHandlerTrait;
23
24
    /**
25
     * @param resource|string $input
26
     *
27
     * @throws YaEtlException
28
     * @throws NodalFlowException
29
     */
30
    public function __construct($input)
31
    {
32
        if (is_resource($input)) {
33
            $metaData = stream_get_meta_data($input);
34
            if (!is_writable($metaData['uri'])) {
35
                throw new YaEtlException('CsvLoader : destination cannot be opened in write mode');
36
            }
37
        }
38
39
        $this->initHandle($input, 'wb');
40
        parent::__construct();
41
    }
42
43
    /**
44
     * @return $this
45
     */
46
    public function writeBom()
47
    {
48
        if ($this->useBom && ($bom = $this->prependBom(''))) {
49
            fwrite($this->handle, $bom);
50
        }
51
52
        return $this;
53
    }
54
}
55