Passed
Pull Request — 2.x (#381)
by Aleksei
19:09
created

TupleStorage::detach()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Transaction;
6
7
use Countable;
8
use IteratorAggregate;
9
use SplObjectStorage;
10
11
/**
12
 * @internal
13
 * @implements IteratorAggregate<object, Tuple>
14
 */
15
final class TupleStorage implements IteratorAggregate, Countable
16
{
17
    /** @var SplObjectStorage<object, Tuple> */
18
    private SplObjectStorage $storage;
19
20
    public function __construct()
21
    {
22
        $this->storage = new SplObjectStorage();
23
    }
24
25
    public function getIterator(): \Traversable
26
    {
27
        $this->storage->rewind();
28
29
        while ($this->storage->valid()) {
30
            $entity = $this->storage->current();
31
            $tuple = $this->storage->getInfo();
32
            $this->storage->next();
33
            yield $entity => $tuple;
34
        }
35
    }
36
37
    /**
38
     * Returns {@see Tuple} if exists, throws an exception otherwise.
39
     *
40
     * @throws \Throwable if the entity is not found in the storage
41
     */
42
    public function getTuple(object $entity): Tuple
43
    {
44
        return $this->storage->offsetGet($entity);
45
    }
46
47
    public function attach(Tuple $tuple): void
48
    {
49
        $this->storage->attach($tuple->entity, $tuple);
50
    }
51
52
    public function contains(object $entity): bool
53
    {
54
        return $this->storage->contains($entity);
55
    }
56
57
    public function detach(object $entity): void
58
    {
59
        $this->storage->offsetUnset($entity);
60
    }
61
62
    /**
63
     * @return int<0, max>
64
     */
65
    public function count(): int
66
    {
67
        return $this->storage->count();
68
    }
69
}
70