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 ( 783dcd...e1577a )
by joseph
26s queued 15s
created

TestEntityGeneratorFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Test Coverage

Coverage 39.05%

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 188
ccs 41
cts 105
cp 0.3905
rs 10
c 0
b 0
f 0
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 1
A ensureMetaDataLoaded() 0 3 1
A createForEntityFqn() 0 15 1
A getFakerDataFillerForEntityFqn() 0 3 1
A getEntityDsm() 0 13 2
A setSeed() 0 5 1
A setFakerDataProviderClasses() 0 5 1
A getFakerDataProvidersFromEntityFqn() 0 46 3
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Mapping\ClassMetadata;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
8
use EdmondsCommerce\DoctrineStaticMeta\DoctrineStaticMeta;
9
use EdmondsCommerce\DoctrineStaticMeta\Entity\DataTransferObjects\DtoFactory;
10
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactoryInterface;
11
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory;
12
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityDataValidatorFactory;
13
use EdmondsCommerce\DoctrineStaticMeta\Exception\TestConfigurationException;
14
use function class_exists;
15
use function defined;
16
17
/**
18
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19
 */
20
class TestEntityGeneratorFactory
21
{
22
    /**
23
     * @var EntitySaverFactory
24
     */
25
    protected $entitySaverFactory;
26
    /**
27
     * @var EntityDataValidatorFactory
28
     */
29
    protected $entityValidatorFactory;
30
    /**
31
     * @var array
32
     */
33
    protected $fakerDataProviderClasses;
34
    /**
35
     * @var float|null
36
     */
37
    protected $seed;
38
    /**
39
     * @var EntityFactoryInterface|null
40
     */
41
    protected $entityFactory;
42
    /**
43
     * @var DtoFactory
44
     */
45
    private $dtoFactory;
46
    /**
47
     * @var EntityManagerInterface
48
     */
49
    private $entityManager;
50
    /**
51
     * @var NamespaceHelper
52
     */
53
    private $namespaceHelper;
54
    /**
55
     * @var FakerDataFillerFactory
56
     */
57
    private $fakerDataFillerFactory;
58
59 1
    public function __construct(
60
        EntitySaverFactory $entitySaverFactory,
61
        EntityFactoryInterface $entityFactory,
62
        DtoFactory $dtoFactory,
63
        EntityManagerInterface $entityManager,
64
        NamespaceHelper $namespaceHelper,
65
        FakerDataFillerFactory $fakerDataFillerFactory,
66
        array $fakerDataProviderClasses = null,
67
        ?float $seed = null
68
    ) {
69 1
        $this->entitySaverFactory       = $entitySaverFactory;
70 1
        $this->entityFactory            = $entityFactory;
71 1
        $this->dtoFactory               = $dtoFactory;
72 1
        $this->entityManager            = $entityManager;
73 1
        $this->namespaceHelper          = $namespaceHelper;
74 1
        $this->fakerDataProviderClasses = $fakerDataProviderClasses;
75 1
        $this->seed                     = $seed;
76 1
        $this->fakerDataFillerFactory   = $fakerDataFillerFactory;
77 1
        $this->fakerDataFillerFactory->setSeed($seed);
78 1
        $this->fakerDataFillerFactory->setFakerDataProviders($fakerDataProviderClasses);
79 1
        $this->ensureMetaDataLoaded();
80 1
    }
81
82 1
    private function ensureMetaDataLoaded(): void
83
    {
84 1
        $this->entityManager->getMetadataFactory()->getAllMetadata();
85 1
    }
86
87 1
    public function createForEntityFqn(
88
        string $entityFqn,
89
        EntityManagerInterface $entityManager = null
90
    ): TestEntityGenerator {
91 1
        $this->fakerDataFillerFactory->setFakerDataProviders(
92 1
            $this->fakerDataProviderClasses ?? $this->getFakerDataProvidersFromEntityFqn($entityFqn)
93
        );
94
95 1
        return new TestEntityGenerator(
96 1
            $this->getEntityDsm($entityFqn),
97 1
            $this->entityFactory,
0 ignored issues
show
Bug introduced by
It seems like $this->entityFactory can also be of type null; however, parameter $entityFactory of EdmondsCommerce\Doctrine...enerator::__construct() does only seem to accept EdmondsCommerce\Doctrine...\EntityFactoryInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
            /** @scrutinizer ignore-type */ $this->entityFactory,
Loading history...
98 1
            $this->dtoFactory,
99 1
            $this,
100 1
            $this->getFakerDataFillerForEntityFqn($entityFqn),
101 1
            $entityManager ?? $this->entityManager
102
        );
103
    }
104
105
    /**
106
     * Get the list of Faker data providers for the project
107
     *
108
     * By convention this is stored as a constant array on the project level AbstractEntityTest and is generated as
109
     * part of the DSM code generation
110
     *
111
     * This method will throw detailed exceptions if the abstract entity test is not found
112
     *
113
     * @param string $entityFqn
114
     *
115
     * @return array|string[]
116
     * @throws TestConfigurationException
117
     */
118 1
    private function getFakerDataProvidersFromEntityFqn(string $entityFqn): array
119
    {
120 1
        $projectRootNamespace = $this->namespaceHelper->getProjectNamespaceRootFromEntityFqn($entityFqn);
121 1
        $abstractTestFqn      = $this->namespaceHelper->tidy(
122 1
            $projectRootNamespace . '\\Entities\\AbstractEntityTest'
123
        );
124 1
        if (!class_exists($abstractTestFqn)) {
125
            throw new TestConfigurationException(<<<TEXT
126
Failed finding the AbstractEntityTest: $abstractTestFqn
127
128
This could means that your composer configuration is not correct with regards to 
129
including the abstract entity test that has all the definitions for faker data
130
131
You need something that looks like:
132
133
```
134
  "autoload-dev": {
135
    "psr-4": {
136
      "My\\Project\\": [
137
        "tests/"
138
      ],
139
      "My\\Entities\\Assets\\": [
140
        "vendor/my/entities/tests/Assets/"
141
      ]
142
    },
143
    "files": [
144
      "vendor/my/entities/tests/Entities/AbstractEntityTest.php"
145
    ]
146
  },
147
```
148
149
TEXT
150
            );
151
        }
152 1
        if (!defined($abstractTestFqn . '::FAKER_DATA_PROVIDERS')) {
153
            throw new TestConfigurationException(<<<TEXT
154
Your AbstractEntityTest ($abstractTestFqn) does not have the FAKER_DATA_PROVIDERS constant.
155
 
156
This means that you will not get any custom faker data which is essential to ensure 
157
your generated entities can pass their own validation and be persisted
158
159
TEXT
160
            );
161
        }
162
163 1
        return $abstractTestFqn::FAKER_DATA_PROVIDERS;
164
    }
165
166 1
    private function getEntityDsm(string $entityFqn): DoctrineStaticMeta
167
    {
168
        /**
169
         * @var DoctrineStaticMeta $dsm
170
         */
171 1
        $dsm      = $entityFqn::getDoctrineStaticMeta();
172 1
        $metaData = $this->entityManager->getMetadataFactory()->getMetadataFor($entityFqn);
173 1
        if ($metaData instanceof ClassMetadata) {
174 1
            $dsm->setMetaData($metaData);
175
176 1
            return $dsm;
177
        }
178
        throw new \RuntimeException('$metaData is not an instance of ClassMetadata');
179
    }
180
181 1
    private function getFakerDataFillerForEntityFqn(string $entityFqn): FakerDataFillerInterface
182
    {
183 1
        return $this->fakerDataFillerFactory->getInstanceFromEntityFqn($entityFqn);
184
    }
185
186
    /**
187
     * @param float $seed
188
     *
189
     * @return TestEntityGeneratorFactory
190
     */
191
    public function setSeed(float $seed): TestEntityGeneratorFactory
192
    {
193
        $this->seed = $seed;
194
195
        return $this;
196
    }
197
198
    /**
199
     * @param array $fakerDataProviderClasses
200
     *
201
     * @return TestEntityGeneratorFactory
202
     */
203
    public function setFakerDataProviderClasses(array $fakerDataProviderClasses): TestEntityGeneratorFactory
204
    {
205
        $this->fakerDataProviderClasses = $fakerDataProviderClasses;
206
207
        return $this;
208
    }
209
}
210