Base   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 65
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getActiveSheet() 0 7 3
A __construct() 0 6 2
A fetchData() 0 6 1
A yieldData() 0 4 2
A asArray() 0 3 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Infrastructure Related Agent
5
 * @author Max Demian <[email protected]>
6
 */
7
8
namespace Ticaje\FileManager\Implementors\Reader\File\BoxSpout;
9
10
use Box\Spout\Common\Exception\UnsupportedTypeException;
11
use Box\Spout\Reader\Common\Creator\ReaderFactory;
12
use Box\Spout\Reader\Exception\ReaderNotOpenedException;
13
use Box\Spout\Reader\ReaderInterface;
14
use Box\Spout\Reader\SheetInterface;
15
use Exception;
16
use Generator;
17
use Iterator;
18
use Ticaje\FileManager\Infrastructure\Driver\Reader\Interfaces\Gateway\FileGatewayInterface;
19
20
/**
21
 * Class Base
22
 * @package Ticaje\FileManager\Implementors\Reader\File\BoxSpout
23
 */
24
abstract class Base implements FileGatewayInterface
25
{
26
    /** @var ReaderInterface $concretion */
27
    protected $concretion;
28
29
    /** @var string $type */
30
    protected $type;
31
32
    /**
33
     * Base constructor.
34
     * @throws Exception
35
     */
36
    public function __construct()
37
    {
38
        try {
39
            $this->concretion = ReaderFactory::createFromType($this->type);
40
        } catch (UnsupportedTypeException $exception) {
41
            throw new Exception("Reader Agent Class does not exist");
42
        }
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function fetchData(string $fileName): Iterator
49
    {
50
        $this->concretion->open($fileName);
51
        $sheet = $this->getActiveSheet();
52
53
        return $this->yieldData($sheet);
54
    }
55
56
    /**
57
     * @return SheetInterface
58
     * @throws ReaderNotOpenedException
59
     */
60
    private function getActiveSheet(): SheetInterface
61
    {
62
        $sheets = $this->concretion->getSheetIterator();
63
        /** @var Sheet $sheet */
64
        foreach ($sheets as $sheet) {
65
            if ($sheet->isActive()) {
66
                return $sheet;
67
            }
68
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Box\Spout\Reader\SheetInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
69
    }
70
71
    /**
72
     * @param SheetInterface $sheet
73
     *
74
     * @return Generator
75
     */
76
    private function yieldData(SheetInterface $sheet)
77
    {
78
        foreach ($sheet->getRowIterator() as $row) {
79
            yield $row;
80
        }
81
    }
82
83
    /**
84
     * @inheritDoc
85
     */
86
    public function asArray($object)
87
    {
88
        return is_array($object) ? $object : $object->toArray();
89
    }
90
}
91