Base   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getImplementor() 0 3 1
A __construct() 0 6 1
A setSource() 0 7 1
A getContentCopy() 0 3 1
A getData() 0 9 4
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