Passed
Push — master ( d0ce80...210dba )
by Jakub
06:27
created

CharacterEffectsCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nette\Utils\Arrays;
7
8
/**
9
 * @author Jakub Konečný
10
 * @internal
11
 */
12
final class CharacterEffectsCollection extends \Nexendrie\Utils\Collection {
13
  protected string $class = CharacterEffect::class;
14
  protected Character $character;
15
16
  public function __construct(Character $character) {
17 1
    parent::__construct();
18 1
    $this->character = $character;
19 1
  }
20
21
  /**
22
   * @param int|NULL $index
23
   * @param CharacterEffect $item
24
   * @throws \OutOfRangeException
25
   * @throws \InvalidArgumentException
26
   * @throws \RuntimeException
27
   */
28
  public function offsetSet($index, $item): void {
29 1
    parent::offsetSet($index, $item);
30 1
    $item->onApply($this->character, $item);
31 1
  }
32
33
  /**
34
   * @param int $index
35
   * @throws \RuntimeException
36
   * @throws \OutOfRangeException
37
   */
38
  public function offsetUnset($index): void {
39
    try {
40
      /** @var CharacterEffect $item */
41 1
      $item = Arrays::get($this->items, $index);
42
    } catch(\Nette\InvalidArgumentException $e) {
43
      throw new \OutOfRangeException("Offset invalid or out of range.");
44
    }
45 1
    parent::offsetUnset($index);
46 1
    $item->onRemove($this->character, $item);
47 1
  }
48
}
49
?>