SaveEntityTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 60.87%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 39
ccs 14
cts 23
cp 0.6087
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A saveAllInTransaction() 0 16 3
A saveAll() 0 9 2
A deleteAll() 0 9 2
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