Passed
Push — etls ( 3c161b )
by Fabrice
11:34
created

StreamRead   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 70
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSourcePath() 0 3 1
A getSourceFilePath() 0 3 1
A setSourcePath() 0 5 1
A getSourceFileName() 0 3 1
A getSourceStream() 0 7 3
A setSourceStream() 0 9 2
A setSourceFileName() 0 5 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\Etl\Streams\Traits;
11
12
use fab2s\YaEtl\YaEtlException;
13
14
trait StreamRead
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $sourcePath = '';
20
21
    /**
22
     * @var string
23
     */
24
    protected $sourceFileName;
25
26
    /**
27
     * @var resource|null
28
     */
29
    protected $sourceStream = null;
30
31
    public function setSourcePath(string $sourcePath): self
32
    {
33
        $this->sourcePath = $this->slashPath($sourcePath);
34
35
        return $this;
36
    }
37
38
    public function getSourcePath(): string
39
    {
40
        return $this->sourcePath;
41
    }
42
43
    public function setSourceStream($resource): self
44
    {
45
        if (!is_resource($resource)) {
46
            throw new YaEtlException('[' . class_basename(static::class) . '] Invalid handle');
47
        }
48
49
        $this->sourceStream = $resource;
50
51
        return $this;
52
    }
53
54
    /**
55
     * @return null|false|resource
56
     */
57
    public function getSourceStream(bool $fresh = false)
58
    {
59
        if (!$fresh && !empty($this->sourceStream)) {
60
            return $this->sourceStream;
61
        }
62
63
        return $this->sourceStream = fopen($this->getSourceFilePath(), 'rb');
64
    }
65
66
    public function getSourceFileName(): string
67
    {
68
        return $this->sourceFileName;
69
    }
70
71
    public function setSourceFileName($sourceFileName): self
72
    {
73
        $this->sourceFileName = basename($sourceFileName);
74
75
        return $this;
76
    }
77
78
    public function getSourceFilePath(): string
79
    {
80
        return $this->getSourcePath() . $this->getSourceFileName();
81
    }
82
83
    abstract public function slashPath(string $path) : string;
84
}
85