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

StreamWrite::getDestinationFileName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 9
rs 10
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
trait StreamWrite
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $destinationPath = '';
18
19
    /**
20
     * @var string
21
     */
22
    protected $destinationFileName;
23
24
    /**
25
     * @var resource|null
26
     */
27
    protected $destinationStream = null;
28
29
    public function getDestinationFileName(): string
30
    {
31
        if (!isset($this->destinationFileName)) {
32
            if (is_callable([$this, 'getSourceFileName'])) {
33
                return $this->destinationFileName = $this->getSourceFileName();
0 ignored issues
show
Bug introduced by
It seems like getSourceFileName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
                return $this->destinationFileName = $this->/** @scrutinizer ignore-call */ getSourceFileName();
Loading history...
34
            }
35
        }
36
37
        return $this->destinationFileName;
38
    }
39
40
    public function setDestinationFileName(string $destinationFileName): self
41
    {
42
        $this->destinationFileName = basename($destinationFileName);
43
44
        return $this;
45
    }
46
    /**
47
     * @return null|false|resource
48
     */
49
    public function getDestinationStream(bool $fresh = false)
50
    {
51
        if (!$fresh && !empty($this->destinationStream)) {
52
            return $this->destinationStream;
53
        }
54
55
        return $this->destinationStream = fopen($this->getDestinationFilePath(), 'wb');
56
    }
57
58
    public function getDestinationFilePath(): string
59
    {
60
        return $this->getDestinationPath() . $this->getDestinationFileName();
61
    }
62
63
    public function getDestinationPath(): string
64
    {
65
        return $this->destinationPath;
66
    }
67
68
    public function setDestinationPath(string $destinationPath): self
69
    {
70
        $this->destinationPath =  $this->slashPath($destinationPath);
71
72
        return $this;
73
    }
74
75
    abstract public function slashPath(string $path) : string;
76
}
77