Issues (188)

src/Transaction.php (1 issue)

Severity
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
    public function persist(object $entity, int $mode = self::MODE_CASCADE): self
29 2724
    {
30
        $this->initUow()->persistDeferred($entity, $mode === self::MODE_CASCADE);
31 2724
32
        return $this;
33 2724
    }
34
35
    public function delete(object $entity, int $mode = self::MODE_CASCADE): self
36 168
    {
37
        $this->initUow()->delete($entity, $mode === self::MODE_CASCADE);
38 168
39
        return $this;
40 168
    }
41
42
    public function run(): void
43 2828
    {
44
        if ($this->uow === null) {
45 2828
            return;
46
        }
47
        $uow = $this->uow->run();
48 2828
        $this->uow = null;
49 2828
50
        if (!$uow->isSuccess()) {
51 2828
            throw $uow->getLastError();
52 120
        }
53
    }
54
55
    private function initUow(): UnitOfWork
56 2828
    {
57
        $this->uow ??= new UnitOfWork($this->orm, $this->runner);
58 2828
        return $this->uow;
59 2828
    }
60
}
61