Test Failed
Branch 4.x (8bbc02)
by Loïc
02:21
created

JsonReader::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the DataImporter package.
7
 *
8
 * (c) Loïc Sapone <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace IQ2i\DataImporter\Reader;
15
16
class JsonReader implements ReaderInterface
17
{
18
    private readonly \SplFileInfo $file;
19
20
    private readonly \ArrayIterator $iterator;
21
22
    private int $index = 1;
23
24
    public function __construct(
25
        string $filePath,
26
        private readonly ?string $dto = null,
27
    ) {
28
        $this->file = new \SplFileInfo($filePath);
0 ignored issues
show
Bug introduced by
The property file is declared read-only in IQ2i\DataImporter\Reader\JsonReader.
Loading history...
29
30
        if (!$this->file->isReadable()) {
31
            throw new \InvalidArgumentException('The file '.$this->file->getFilename().' is not readable.');
32
        }
33
34
        $array = \json_decode(\file_get_contents($this->file->getRealPath()), true, 512, \JSON_THROW_ON_ERROR);
35
        $this->iterator = new \ArrayIterator($array);
0 ignored issues
show
Bug introduced by
The property iterator is declared read-only in IQ2i\DataImporter\Reader\JsonReader.
Loading history...
36
37
        $this->rewind();
38
    }
39
40
    public function getDto(): ?string
41
    {
42
        return $this->dto;
43
    }
44
45
    public function isDenormalizable(): bool
46
    {
47
        return null !== $this->dto;
48
    }
49
50
    public function getFile(): \SplFileInfo
51
    {
52
        return $this->file;
53
    }
54
55
    public function index(): mixed
56
    {
57
        return $this->index;
58
    }
59
60
    public function current(): array
61
    {
62
        if (!$this->valid()) {
63
            return [];
64
        }
65
66
        return $this->iterator->current();
67
    }
68
69
    public function next(): void
70
    {
71
        $this->iterator->next();
72
        ++$this->index;
73
    }
74
75
    public function key(): mixed
76
    {
77
        return $this->iterator->key();
78
    }
79
80
    public function valid(): bool
81
    {
82
        return $this->iterator->valid();
83
    }
84
85
    public function rewind(): void
86
    {
87
        $this->iterator->rewind();
88
    }
89
90
    public function count(): int
91
    {
92
        return $this->iterator->count();
93
    }
94
}
95