Passed
Push — master ( 7bba5d...c1c3fd )
by Fabrice
02:35
created

FileExtractorAbstract::extract()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 4
nop 1
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\Extractors\File;
11
12
use fab2s\NodalFlow\YaEtlException;
13
use fab2s\YaEtl\Extractors\ExtractorAbstract;
14
15
/**
16
 * Class FileExtractorAbstract
17
 */
18
abstract class FileExtractorAbstract extends ExtractorAbstract
19
{
20
    /**
21
     * @var string
22
     */
23
    protected $srcFile;
24
25
    /**
26
     * @var resource
27
     */
28
    protected $handle;
29
30
    /**
31
     * @param mixed|resource|string $input
32
     *
33
     * @throws YaEtlException
34
     */
35
    public function __construct($input)
36
    {
37
        if (is_resource($input)) {
38
            $this->handle = $input;
39
        } elseif (is_file($input)) {
40
            $this->srcFile = $input;
41
        } else {
42
            throw new YaEtlException('Input is either not a resource or not a file');
43
        }
44
45
        parent::__construct();
46
    }
47
48
    /**
49
     * make sure we do not hold un-necessary handles
50
     */
51
    public function __destruct()
52
    {
53
        $this->releaseHandle();
54
    }
55
56
    /**
57
     * @param mixed $param
58
     *
59
     * @return bool
60
     */
61
    public function extract($param = null)
62
    {
63
        if ($this->srcFile !== null) {
64
            $this->handle = fopen($this->srcFile, 'rb');
65
        }
66
67
        if (!$this->handle | [!is_resource($this->handle)]) {
68
            return false;
69
        }
70
71
        return rewind($this->handle);
72
    }
73
74
    /**
75
     * release handle
76
     *
77
     * @return $this
78
     */
79
    public function releaseHandle()
80
    {
81
        if (is_resource($this->handle)) {
82
            fclose($this->handle);
83
        }
84
85
        $this->handle = null;
86
87
        return $this;
88
    }
89
}
90