Passed
Branch 4.x (8bbc02)
by Loïc
02:29
created

JsonReader::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 1
    public function __construct(
25
        string $filePath,
26
        private readonly ?string $dto = null,
27
    ) {
28 1
        $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 1
        if (!$this->file->isReadable()) {
31
            throw new \InvalidArgumentException('The file '.$this->file->getFilename().' is not readable.');
32
        }
33
34 1
        $array = \json_decode(\file_get_contents($this->file->getRealPath()), true, 512, \JSON_THROW_ON_ERROR);
35 1
        $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 1
        $this->rewind();
38
    }
39
40
    public function getDto(): ?string
41
    {
42
        return $this->dto;
43
    }
44
45 1
    public function isDenormalizable(): bool
46
    {
47 1
        return null !== $this->dto;
48
    }
49
50 1
    public function getFile(): \SplFileInfo
51
    {
52 1
        return $this->file;
53
    }
54
55 1
    public function index(): mixed
56
    {
57 1
        return $this->index;
58
    }
59
60 1
    public function current(): array
61
    {
62 1
        if (!$this->valid()) {
63 1
            return [];
64
        }
65
66 1
        return $this->iterator->current();
67
    }
68
69 1
    public function next(): void
70
    {
71 1
        $this->iterator->next();
72 1
        ++$this->index;
73
    }
74
75 1
    public function key(): mixed
76
    {
77 1
        return $this->iterator->key();
78
    }
79
80 1
    public function valid(): bool
81
    {
82 1
        return $this->iterator->valid();
83
    }
84
85 1
    public function rewind(): void
86
    {
87 1
        $this->iterator->rewind();
88
    }
89
90 1
    public function count(): int
91
    {
92 1
        return $this->iterator->count();
93
    }
94
}
95