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
Push — master ( c22d50...0bd9a0 )
by joseph
18s queued 11s
created

AbstractEntityFixtureLoader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures;
4
5
use Doctrine\Common\DataFixtures\AbstractFixture;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Doctrine\ORM\EntityManagerInterface;
8
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
9
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory;
10
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\TestEntityGenerator;
11
12
abstract class AbstractEntityFixtureLoader extends AbstractFixture
13
{
14
    public const BULK_AMOUNT_TO_GENERATE = 100;
15
    /**
16
     * @var TestEntityGenerator
17
     */
18
    protected $testEntityGenerator;
19
    /**
20
     * @var EntitySaverFactory
21
     */
22
    protected $saverFactory;
23
24
    /**
25
     * @var null|FixtureEntitiesModifierInterface
26
     */
27
    protected $modifier;
28
29
    /**
30
     * @var string
31
     */
32
    protected $entityFqn;
33
34 3
    public function __construct(
35
        TestEntityGenerator $testEntityGenerator,
36
        EntitySaverFactory $saverFactory,
37
        ?FixtureEntitiesModifierInterface $modifier = null
38
    ) {
39 3
        $this->testEntityGenerator = $testEntityGenerator;
40 3
        $this->saverFactory        = $saverFactory;
41 3
        $this->entityFqn           = $this->getEntityFqn();
42 3
        if (null !== $modifier) {
43 1
            $this->setModifier($modifier);
44
        }
45 3
    }
46
47
    /**
48
     * Use this method to inject your own modifier that will receive the array of generated entities and can then
49
     * update them as you see fit
50
     *
51
     * @param FixtureEntitiesModifierInterface $modifier
52
     */
53 1
    public function setModifier(FixtureEntitiesModifierInterface $modifier): void
54
    {
55 1
        $this->modifier = $modifier;
56 1
    }
57
58
59
    /**
60
     * Load data fixtures with the passed EntityManager
61
     *
62
     * @param ObjectManager $manager
63
     *
64
     * @throws \Doctrine\ORM\Mapping\MappingException
65
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
66
     * @throws \ErrorException
67
     * @throws \ReflectionException
68
     */
69 2
    public function load(ObjectManager $manager)
70
    {
71 2
        if (!$manager instanceof EntityManagerInterface) {
72
            throw new \RuntimeException(
73
                'Expecting $manager to be EntityManagerInterface but got ' . \get_class($manager)
74
            );
75
        }
76 2
        $entities = $this->loadBulk($manager);
77 2
        $this->updateGenerated($entities);
78 2
        $this->saverFactory->getSaverForEntityFqn($this->entityFqn)->saveAll($entities);
79 2
    }
80
81 2
    protected function updateGenerated(array &$entities)
82
    {
83 2
        if (null === $this->modifier) {
84 1
            return;
85
        }
86 1
        $this->modifier->modifyEntities($entities);
87 1
    }
88
89
    /**
90
     * @param EntityManagerInterface $entityManager
91
     *
92
     * @return array|EntityInterface[]
93
     * @throws \Doctrine\ORM\Mapping\MappingException
94
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
95
     * @throws \ErrorException
96
     * @throws \ReflectionException
97
     */
98 2
    protected function loadBulk(EntityManagerInterface $entityManager): array
99
    {
100 2
        $entities = $this->testEntityGenerator->generateEntities(
101 2
            $entityManager,
102 2
            $this->entityFqn,
103 2
            static::BULK_AMOUNT_TO_GENERATE
104
        );
105 2
        foreach ($entities as $generated) {
106 2
            $this->testEntityGenerator->addAssociationEntities($entityManager, $generated);
107
        }
108
109 2
        return $entities;
110
    }
111
112
    /**
113
     * Get the fully qualified name of the Entity we are testing,
114
     * assumes EntityNameTest as the entity class short name
115
     *
116
     * @return string
117
     */
118 3
    protected function getEntityFqn(): string
119
    {
120 3
        if (null === $this->entityFqn) {
121 3
            $this->entityFqn = \str_replace(
122 3
                '\\Assets\\EntityFixtures\\',
123 3
                '\\Entities\\',
124 3
                \substr(static::class, 0, -7)
125
            );
126
        }
127
128 3
        return $this->entityFqn;
129
    }
130
}
131