GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#57)
by Ross
16:56
created

AbstractEntitySpecificSaver::getEntityFqn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Savers;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
7
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\PrimaryKey\IdFieldInterface;
8
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
9
10
abstract class AbstractEntitySpecificSaver extends EntitySaver
11
{
12
    /**
13
     * @var EntityManagerInterface
14
     */
15
    protected $entityManager;
16
17
    /**
18
     * @var string
19
     */
20
    protected $entityFqn;
21
    /**
22
     * @var NamespaceHelper
23
     */
24
    protected $namespaceHelper;
25
26 6
    public function __construct(EntityManagerInterface $entityManager, NamespaceHelper $namespaceHelper)
27
    {
28 6
        parent::__construct($entityManager);
29 6
        $this->namespaceHelper = $namespaceHelper;
30 6
    }
31
32
33
    /**
34
     *
35
     *
36
     * @param array $entities
37
     *
38
     * @throws DoctrineStaticMetaException
39
     * @throws \ReflectionException
40
     */
41 2
    public function saveAll(array $entities): void
42
    {
43 2
        if (empty($entities)) {
44
            return;
45
        }
46
47 2
        foreach ($entities as $entity) {
48 2
            $this->checkIsCorrectEntityType($entity);
49 2
            $this->entityManager->persist($entity);
50
        }
51
52 2
        $this->entityManager->flush();
53 2
    }
54
55
56
    /**
57
     * @param array $entities
58
     *
59
     * @throws DoctrineStaticMetaException
60
     * @throws \ReflectionException
61
     */
62 2
    public function removeAll(array $entities): void
63
    {
64 2
        foreach ($entities as $entity) {
65 2
            $this->checkIsCorrectEntityType($entity);
66 2
            $this->entityManager->remove($entity);
67
        }
68
69 2
        $this->entityManager->flush();
70 2
    }
71
72
    /**
73
     * @param IdFieldInterface $entity
74
     *
75
     * @return void
76
     * @throws DoctrineStaticMetaException
77
     * @throws \ReflectionException
78
     */
79 4
    protected function checkIsCorrectEntityType(IdFieldInterface $entity): void
80
    {
81 4
        $entityFqn = $this->getEntityFqn();
82
83 4
        if (!$entity instanceof $entityFqn) {
84
            $ref = new \ReflectionClass($entity);
85
            $msg = "[ {$ref->getName()} ] is not an instance of [ $entityFqn ]";
86
            throw new DoctrineStaticMetaException($msg);
87
        }
88 4
    }
89
90
    /**
91
     * Based on the convention that the Entity Specific Saver namespace has been generated,
92
     * We can do some simple find/replace to get the Entity namespace
93
     *
94
     * @return string
95
     */
96 4
    protected function getEntityFqn(): string
97
    {
98 4
        if (null === $this->entityFqn) {
99 4
            $this->entityFqn = \str_replace(
100 4
                '\\Entity\\Savers\\',
101 4
                '\\Entities\\',
102 4
                $this->namespaceHelper->cropSuffix(static::class, 'Saver')
103
            );
104
        }
105
106 4
        return $this->entityFqn;
107
    }
108
}
109