Passed
Push — 2.x ( 5e81a5...d097d1 )
by Aleksei
14:17
created

Transaction::initUow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM;
6
7
use Cycle\ORM\Transaction\RunnerInterface;
8
use Cycle\ORM\Transaction\UnitOfWork;
9
10
/**
11
 * Transaction provides ability to define set of entities to be stored or deleted within one transaction. Transaction
12
 * can operate as UnitOfWork. Multiple transactions can co-exists in one application.
13
 *
14
 * Internally, upon "run", transaction will request mappers to generate graph of linked commands to create, update or
15
 * delete entities.
16
 *
17
 * @deprecated since 2.0 use {@see \Cycle\ORM\EntityManager}
18
 */
19
final class Transaction implements TransactionInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Cycle\ORM\TransactionInterface has been deprecated: since 2.0 use {@see \Cycle\ORM\EntityManagerInterface} ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

19
final class Transaction implements /** @scrutinizer ignore-deprecated */ TransactionInterface

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
20
{
21
    private ?UnitOfWork $uow = null;
22
23 2828
    public function __construct(
24
        private ORMInterface $orm,
25
        private ?RunnerInterface $runner = null
26
    ) {
27
    }
28
29 2724
    public function persist(object $entity, int $mode = self::MODE_CASCADE): self
30
    {
31 2724
        $this->initUow()->persistDeferred($entity, $mode === self::MODE_CASCADE);
32
33 2724
        return $this;
34
    }
35
36 168
    public function delete(object $entity, int $mode = self::MODE_CASCADE): self
37
    {
38 168
        $this->initUow()->delete($entity, $mode === self::MODE_CASCADE);
39
40 168
        return $this;
41
    }
42
43 2828
    public function run(): void
44
    {
45 2828
        if ($this->uow === null) {
46
            return;
47
        }
48 2828
        $uow = $this->uow->run();
49 2828
        $this->uow = null;
50
51 2828
        if (!$uow->isSuccess()) {
52 120
            throw $uow->getLastError();
53
        }
54
    }
55
56 2828
    private function initUow(): UnitOfWork
57
    {
58 2828
        $this->uow ??= new UnitOfWork($this->orm, $this->runner);
59 2828
        return $this->uow;
60
    }
61
}
62