Passed
Push — master ( 07f27f...cc2af6 )
by Anton
04:06 queued 10s
created

PersistRepository::deleteByPK()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Cycle\ORM\Select;
4
5
use Cycle\ORM\ORMInterface;
6
use Cycle\ORM\Select;
7
use Cycle\ORM\Transaction;
8
9
class PersistRepository extends Repository
10
{
11
    /** @var Transaction */
12
    private $transaction;
13
14
    /**
15
     * @param Select $select
16
     * @param ORMInterface $orm
17
     */
18
    public function __construct(
19
        Select $select,
20
        ORMInterface $orm
21
    ) {
22
        parent::__construct($select);
23
        $this->transaction = new Transaction($orm);
24
    }
25
26
    /**
27
     * @param mixed $entity
28
     * @param bool $cascade
29
     *
30
     * @throws \Throwable
31
     */
32
    public function save(
33
        $entity,
34
        bool $cascade = true
35
    ): void {
36
        $this->transaction->persist(
37
            $entity,
38
            $cascade ? Transaction::MODE_CASCADE : Transaction::MODE_ENTITY_ONLY
39
        );
40
41
        $this->transaction->run(); // transaction is clean after run
42
    }
43
44
    /**
45
     * @param mixed $entity
46
     * @param bool $cascade
47
     *
48
     * @throws \Throwable
49
     */
50
    public function delete(
51
        $entity,
52
        bool $cascade = true
53
    ): void {
54
        $this->transaction->persist(
55
            $entity,
56
            $cascade ? Transaction::MODE_CASCADE : Transaction::MODE_ENTITY_ONLY
57
        );
58
59
        $this->transaction->run(); // transaction is clean after run
60
    }
61
62
    /**
63
     * @param $id
64
     * @param bool $cascade
65
     *
66
     * @throws \Throwable
67
     */
68
    public function deleteByPK(
69
        $id,
70
        bool $cascade = true
71
    ): void {
72
        $this->delete($this->findByPK($id), $cascade);
73
    }
74
}
75