Passed
Push — master ( bb35d3...bc1c68 )
by Fabrice
03:52
created

FileLoaderAbstract   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 19 5
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 mixed|resource|string $input
26
     *
27
     * @throws YaEtlException
28
     * @throws NodalFlowException
29
     */
30
    public function __construct($input)
31
    {
32
        if (is_resource($input)) {
33
            $this->handle = $input;
34
        } elseif (is_file($input)) {
35
            $this->handle = fopen($input, 'wb');
36
            if (!$this->handle) {
37
                throw new YaEtlException('CsvLoader : destination cannot be opened in write mode');
38
            }
39
        } else {
40
            throw new YaEtlException('Input is either not a resource or not a file');
41
        }
42
43
        $metaData = stream_get_meta_data($this->handle);
44
        if (!is_writable($metaData['uri'])) {
45
            throw new YaEtlException('CsvLoader : destination cannot be opened in write mode');
46
        }
47
48
        parent::__construct();
49
    }
50
}
51