Passed
Push — master ( fc3b2f...844929 )
by Hector Luis
03:20
created

Base::getContentCopy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Infrastructure Related Agent
5
 * @author Max Demian <[email protected]>
6
 */
7
8
namespace Ticaje\FileManager\Infrastructure\Driver\Reader\File;
9
10
use Iterator;
11
use Ticaje\FileManager\Infrastructure\Driver\Reader\Interfaces\Gateway\FileGatewayInterface;
12
use Ticaje\FileManager\Infrastructure\Driver\Reader\Interfaces\SourceInterface;
13
use Ticaje\FileManager\Infrastructure\Driver\Traits\FileInterface as ContractorTrait;
14
15
/**
16
 * Class Base
17
 * @package Ticaje\FileManager\Infrastructure\Driver\Reader\File
18
 */
19
abstract class Base
20
{
21
    use ContractorTrait;
22
23
    /** @var FileGatewayInterface $implementor */
24
    protected $implementor;
25
26
    /** @var bool $hasHeader */
27
    protected $hasHeader;
28
29
    /** @var Iterator|array $copy */
30
    private $copy;
31
32
    /** @var Iterator|array $content */
33
    private $content;
34
35
36
    /**
37
     * Csv constructor.
38
     *
39
     * @param FileGatewayInterface $implementor
40
     * @param bool                 $hasHeader
41
     */
42
    public function __construct(
43
        FileGatewayInterface $implementor,
44
        bool $hasHeader = false
45
    ) {
46
        $this->implementor = $implementor;
47
        $this->hasHeader = $hasHeader;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    public function setSource(string $fileName): SourceInterface
54
    {
55
        $this->content = $this->implementor->fetchData($fileName);
56
        $this->copy = $this->implementor->fetchData($fileName);
57
        $this->setHeader();
58
59
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Ticaje\FileManager\Infra...Driver\Reader\File\Base which is incompatible with the type-hinted return Ticaje\FileManager\Infra...erfaces\SourceInterface.
Loading history...
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function getData(): Iterator
66
    {
67
        $content = $this->getContent();
68
        if ($content instanceof Iterator) {
0 ignored issues
show
introduced by
$content is always a sub-type of Iterator.
Loading history...
69
            return $content;
70
        }
71
        if (is_array($content)) {
72
            foreach ($this->getContent() as $item) {
73
                yield $item;
74
            }
75
        }
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function getImplementor(): FileGatewayInterface
82
    {
83
        return $this->implementor;
84
    }
85
86
    public function getContentCopy(): Iterator
87
    {
88
        return $this->copy;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->copy could return the type array which is incompatible with the type-hinted return Iterator. Consider adding an additional type-check to rule them out.
Loading history...
89
    }
90
}
91