SaveEntityTrait::saveAllInTransaction()   A
last analyzed

Complexity

Conditions 3
Paths 7

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.1825

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 11
cp 0.7272
rs 9.7333
c 0
b 0
f 0
cc 3
nc 7
nop 1
crap 3.1825
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the zibios/sharep.
7
 *
8
 * (c) Zbigniew Ślązak
9
 */
10
11
namespace App\Repository\Traits;
12
13
use App\Entity\EntityInterface;
14
use Doctrine\ORM\EntityManagerInterface;
15
16
trait SaveEntityTrait
17
{
18 12
    public function saveAllInTransaction(EntityInterface ...$entities): void
19
    {
20
        /** @var EntityManagerInterface $entityManager */
21 12
        $entityManager = $this->getEntityManager();
0 ignored issues
show
Bug introduced by
It seems like getEntityManager() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
22 12
        $entityManager->getConnection()->beginTransaction();
23
        try {
24 12
            foreach ($entities as $entity) {
25 12
                $entityManager->persist($entity);
26
            }
27 12
            $entityManager->flush();
28 12
            $entityManager->getConnection()->commit();
29
        } catch (\Throwable $e) {
30
            $entityManager->getConnection()->rollBack();
31
            throw $e;
32
        }
33 12
    }
34
35 2
    public function saveAll(EntityInterface ...$entities): void
36
    {
37
        /** @var EntityManagerInterface $entityManager */
38 2
        $entityManager = $this->getEntityManager();
0 ignored issues
show
Bug introduced by
It seems like getEntityManager() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39 2
        foreach ($entities as $entity) {
40 2
            $entityManager->persist($entity);
41
        }
42 2
        $entityManager->flush();
43 2
    }
44
45
    public function deleteAll(EntityInterface ...$entities): void
46
    {
47
        /** @var EntityManagerInterface $entityManager */
48
        $entityManager = $this->getEntityManager();
0 ignored issues
show
Bug introduced by
It seems like getEntityManager() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
49
        foreach ($entities as $entity) {
50
            $entityManager->remove($entity);
51
        }
52
        $entityManager->flush();
53
    }
54
}
55