Lines
0.00%
0 / 19
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 / 19 | 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 / 8 | 0.00% | 0 / 1 | 12 | |||||
| offsetUnset | 0.00% | 0 / 2 | 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 | ||
| 3 | namespace BO\Zmsdldb\Importer\MySQL\Entity; | |
| 4 | ||
| 5 | /** | |
| 6 | * @implements \ArrayAccess<int|string, Base> | |
| 7 | */ | |
| 8 | class 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): Collection | |
| 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 | return $this; | |
| 42 | } | |
| 43 | ||
| 44 | #[\Override] | |
| 45 | final public function offsetUnset($offset): Collection | |
| 46 | { | |
| 47 | unset($this->entities[$offset]); | |
| 48 | return $this; | |
| 49 | } | |
| 50 | ||
| 51 | #[\Override] | |
| 52 | final public function count(): int | |
| 53 | { | |
| 54 | return count($this->entities); | |
| 55 | } | |
| 56 | ||
| 57 | /** @psalm-api */ | |
| 58 | public function saveEntities(): void | |
| 59 | { | |
| 60 | try { | |
| 61 | foreach ($this->entities as $entity) { | |
| 62 | $entity->save(); | |
| 63 | } | |
| 64 | } catch (\Exception $e) { | |
| 65 | throw $e; | |
| 66 | } | |
| 67 | } | |
| 68 | } |