JsonReader   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 98
ccs 33
cts 36
cp 0.9167
rs 10
wmc 14

11 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 7 2
A valid() 0 3 1
A getFile() 0 3 1
A index() 0 3 1
A isDenormalizable() 0 3 1
A getDto() 0 3 1
A next() 0 4 1
A rewind() 0 3 1
A key() 0 3 1
A count() 0 3 1
A __construct() 0 24 3
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
use JsonMachine\Items;
17
use JsonMachine\JsonDecoder\ExtJsonDecoder;
18
19
class JsonReader implements ReaderInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    final public const POINTER = 'pointer';
25
26
    private readonly \SplFileInfo $file;
27
28
    private readonly \Iterator $iterator;
29
30
    private int $index = 1;
31
32
    private int $count = 0;
33
34
    private array $defaultContext = [
35
        self::POINTER => null,
36
    ];
37
38 2
    public function __construct(
39
        string $filePath,
40
        private readonly ?string $dto = null,
41
        array $defaultContext = [],
42
    ) {
43 2
        $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...
44
45 2
        if (!$this->file->isReadable()) {
46
            throw new \InvalidArgumentException('The file '.$this->file->getFilename().' is not readable.');
47
        }
48
49 2
        $this->defaultContext = \array_merge($this->defaultContext, $defaultContext);
50 2
        $options = [
51 2
            'decoder' => new ExtJsonDecoder(true),
52 2
        ];
53
54 2
        if (null !== $this->defaultContext[self::POINTER]) {
55 1
            $options['pointer'] = $this->defaultContext[self::POINTER];
56
        }
57
58 2
        $this->count = \iterator_count(Items::fromFile($this->file->getRealPath(), $options));
59 2
        $this->iterator = Items::fromFile($this->file->getRealPath(), $options)->getIterator();
0 ignored issues
show
Bug introduced by
The property iterator is declared read-only in IQ2i\DataImporter\Reader\JsonReader.
Loading history...
60
61 2
        $this->rewind();
62
    }
63
64
    public function getDto(): ?string
65
    {
66
        return $this->dto;
67
    }
68
69 2
    public function isDenormalizable(): bool
70
    {
71 2
        return null !== $this->dto;
72
    }
73
74 2
    public function getFile(): \SplFileInfo
75
    {
76 2
        return $this->file;
77
    }
78
79 2
    public function index(): mixed
80
    {
81 2
        return $this->index;
82
    }
83
84 2
    public function current(): array
85
    {
86 2
        if (!$this->valid()) {
87 2
            return [];
88
        }
89
90 2
        return $this->iterator->current();
91
    }
92
93 2
    public function next(): void
94
    {
95 2
        $this->iterator->next();
96 2
        ++$this->index;
97
    }
98
99 2
    public function key(): mixed
100
    {
101 2
        return $this->iterator->key();
102
    }
103
104 2
    public function valid(): bool
105
    {
106 2
        return $this->iterator->valid();
107
    }
108
109 2
    public function rewind(): void
110
    {
111 2
        $this->iterator->rewind();
112
    }
113
114 2
    public function count(): int
115
    {
116 2
        return $this->count;
117
    }
118
}
119