Lines 0.00% 0 / 17
Functions and Methods 0.00% 0 / 6
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Collection 0.00% 0 / 17 0.00% 0 / 6 132 0.00% 0 / 1
 offsetExists 0.00% 0 / 1 0.00% 0 / 1 2
 offsetGet 0.00% 0 / 3 0.00% 0 / 1 6
 offsetSet 0.00% 0 / 7 0.00% 0 / 1 12
 offsetUnset 0.00% 0 / 1 0.00% 0 / 1 2
 count 0.00% 0 / 1 0.00% 0 / 1 2
 saveEntities 0.00% 0 / 4 0.00% 0 / 1 12
1<?php
2
3namespace BO\Zmsdldb\Importer\MySQL\Entity;
4
5/**
6 * @implements \ArrayAccess<int|string, Base>
7 */
8class Collection implements \Countable, \ArrayAccess
9{
10    /** @var array<int|string, Base> */
11    protected array $entities = [];
12
13    #[\Override]
14    final public function offsetExists($offset): bool
15    {
16        return array_key_exists($offset, $this->entities);
17    }
18
19    #[\Override]
20    final public function offsetGet($offset): Base
21    {
22        if ($this->offsetExists($offset)) {
23            return $this->entities[$offset];
24        }
25        throw new \InvalidArgumentException(__METHOD__ . " offset({$offset}) has not been set!");
26    }
27
28    #[\Override]
29    final public function offsetSet($offset, $value): void
30    {
31        if (!$value instanceof Base) {
32            throw new \InvalidArgumentException(
33                __METHOD__ . ' $value must be an instance of \\BO\\Zmsdldb\\Importer\\MySQL\\Entity\\Base'
34            );
35        }
36        if (null === $offset) {
37            $this->entities[] = $value;
38        } else {
39            $this->entities[$offset] = $value;
40        }
41    }
42
43    #[\Override]
44    final public function offsetUnset($offset): void
45    {
46        unset($this->entities[$offset]);
47    }
48
49    #[\Override]
50    final public function count(): int
51    {
52        return count($this->entities);
53    }
54
55    /** @psalm-api */
56    public function saveEntities(): void
57    {
58        try {
59            foreach ($this->entities as $entity) {
60                $entity->save();
61            }
62        } catch (\Exception $e) {
63            throw $e;
64        }
65    }
66}