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 (#153)
by joseph
29:27
created

AbstractCreator   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 69
dl 0
loc 202
ccs 0
cts 102
cp 0
rs 10
c 0
b 0
f 0
wmc 18

13 Methods

Rating   Name   Duplication   Size   Complexity  
A configurePipeline() 0 5 1
A setTargetContentsWithTemplateContents() 0 5 1
A __construct() 0 13 1
A registerEntityReplaceName() 0 5 1
A createTargetFileObject() 0 18 4
A writeIfNotExists() 0 7 2
A getTargetFile() 0 3 1
A write() 0 5 1
A setProjectRootNamespace() 0 6 1
A updateRootDirOnTargetFile() 0 10 2
A setProjectRootDirectory() 0 6 1
A registerReplaceProjectRootNamespace() 0 5 1
A registerReplaceName() 0 5 1
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation;
4
5
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\Pipeline;
6
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\ReplaceNameProcess;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\ReplaceProjectRootNamespaceProcess;
8
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FileFactory;
9
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FindReplaceFactory;
10
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File;
11
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
12
use EdmondsCommerce\DoctrineStaticMeta\Config;
13
14
/**
15
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
16
 * @SuppressWarnings(PHPMD.NumberOfChildren)
17
 */
18
abstract class AbstractCreator implements CreatorInterface
19
{
20
    protected const TEMPLATE_ENTITY_NAME = 'TemplateEntity';
21
22
    /**
23
     * The absolute path to the template file residing in the root codeTemplates directory
24
     */
25
    protected const TEMPLATE_PATH = 'override me';
26
    /**
27
     * The basename of the Template object FQN
28
     */
29
    protected const FIND_NAME = 'override me';
30
    /**
31
     * The path to the root code templates folder
32
     */
33
    public const ROOT_TEMPLATE_PATH = __DIR__ . '/../../../codeTemplates/';
34
35
    public const SRC_DIR = 'src';
36
37
    public const TEST_DIR = 'tests';
38
39
    /**
40
     * @var string|null
41
     */
42
    protected $newObjectFqn;
43
    /**
44
     * @var File
45
     */
46
    protected $templateFile;
47
    /**
48
     * @var File
49
     */
50
    protected $targetFile;
51
    /**
52
     * @var FileFactory
53
     */
54
    protected $fileFactory;
55
    /**
56
     * @var FindReplaceFactory
57
     */
58
    protected $findReplaceFactory;
59
    /**
60
     * @var NamespaceHelper
61
     */
62
    protected $namespaceHelper;
63
    /**
64
     * @var File\Writer
65
     */
66
    protected $fileWriter;
67
    /**
68
     * @var Pipeline
69
     */
70
    protected $pipeline;
71
    /**
72
     * @var string|null
73
     */
74
    protected $projectRootNamespace;
75
    /**
76
     * @var string|null
77
     */
78
    protected $projectRootDirectory;
79
80
    public function __construct(
81
        FileFactory $fileFactory,
82
        NamespaceHelper $namespaceHelper,
83
        File\Writer $fileWriter,
84
        Config $config,
85
        FindReplaceFactory $findReplaceFactory
86
    ) {
87
        $this->fileFactory     = $fileFactory;
88
        $this->namespaceHelper = $namespaceHelper;
89
        $this->fileWriter      = $fileWriter;
90
        $this->setProjectRootNamespace($this->namespaceHelper->getProjectRootNamespaceFromComposerJson());
91
        $this->setProjectRootDirectory($config::getProjectRootDirectory());
92
        $this->findReplaceFactory = $findReplaceFactory;
93
    }
94
95
    public function setProjectRootNamespace(string $projectRootNamespace): self
96
    {
97
        $this->projectRootNamespace = $projectRootNamespace;
98
        $this->fileFactory->setProjectRootNamespace($projectRootNamespace);
99
100
        return $this;
101
    }
102
103
    public function setProjectRootDirectory(string $projectRootDirectory): self
104
    {
105
        $this->projectRootDirectory = $projectRootDirectory;
106
        $this->fileFactory->setProjectRootDirectory($projectRootDirectory);
107
108
        return $this;
109
    }
110
111
    public function createTargetFileObject(string $newObjectFqn = null): self
112
    {
113
        if (null === $newObjectFqn && null === $this->newObjectFqn) {
114
            throw new \RuntimeException(
115
                'No new objectFqn either set previously or passed in'
116
            );
117
        }
118
        if (null !== $newObjectFqn) {
119
            $this->newObjectFqn = $newObjectFqn;
120
        }
121
        $this->templateFile = $this->fileFactory->createFromExistingPath(static::TEMPLATE_PATH);
122
        $this->targetFile   = $this->fileFactory->createFromFqn($this->newObjectFqn);
0 ignored issues
show
Bug introduced by
It seems like $this->newObjectFqn can also be of type null; however, parameter $fqn of EdmondsCommerce\Doctrine...actory::createFromFqn() does only seem to accept string, 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

122
        $this->targetFile   = $this->fileFactory->createFromFqn(/** @scrutinizer ignore-type */ $this->newObjectFqn);
Loading history...
123
        $this->updateRootDirOnTargetFile();
124
        $this->setTargetContentsWithTemplateContents();
125
        $this->configurePipeline();
126
        $this->pipeline->run($this->targetFile);
127
128
        return $this;
129
    }
130
131
    /**
132
     * Where the template file is in tests, we need to fix that in the target file
133
     */
134
    private function updateRootDirOnTargetFile(): void
135
    {
136
        $realTemplateTestsPath = realpath(self::ROOT_TEMPLATE_PATH . self::TEST_DIR);
137
        if (0 === \strpos($this->templateFile->getPath(), $realTemplateTestsPath)) {
138
            $updatedPath = str_replace(
139
                '/src/',
140
                '/tests/',
141
                $this->targetFile->getPath()
142
            );
143
            $this->targetFile->setPath($updatedPath);
144
        }
145
    }
146
147
    protected function setTargetContentsWithTemplateContents()
148
    {
149
        $this->targetFile->setContents(
150
            $this->templateFile->loadContents()
0 ignored issues
show
Bug introduced by
It seems like $this->templateFile->loa...ntents()->getContents() can also be of type null; however, parameter $contents of EdmondsCommerce\Doctrine...tem\File::setContents() does only seem to accept string, 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

150
            /** @scrutinizer ignore-type */ $this->templateFile->loadContents()
Loading history...
151
                               ->getContents()
152
        );
153
    }
154
155
    /**
156
     * In this method we register all the process steps that we want to rnu against the file
157
     *
158
     * By default this registers the ReplaceNameProcess which is almost certainly required. Other processes can be
159
     * registered as required
160
     */
161
    protected function configurePipeline(): void
162
    {
163
        $this->pipeline = new Pipeline($this->findReplaceFactory);
164
        $this->registerReplaceName();
165
        $this->registerReplaceProjectRootNamespace();
166
    }
167
168
    protected function registerReplaceName(): void
169
    {
170
        $replaceName = new ReplaceNameProcess();
171
        $replaceName->setArgs(static::FIND_NAME, $this->namespaceHelper->basename($this->newObjectFqn));
0 ignored issues
show
Bug introduced by
It seems like $this->newObjectFqn can also be of type null; however, parameter $namespace of EdmondsCommerce\Doctrine...spaceHelper::basename() does only seem to accept string, 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

171
        $replaceName->setArgs(static::FIND_NAME, $this->namespaceHelper->basename(/** @scrutinizer ignore-type */ $this->newObjectFqn));
Loading history...
172
        $this->pipeline->register($replaceName);
173
    }
174
175
    protected function registerReplaceProjectRootNamespace()
176
    {
177
        $replaceTemplateNamespace = new ReplaceProjectRootNamespaceProcess();
178
        $replaceTemplateNamespace->setProjectRootNamespace($this->projectRootNamespace);
0 ignored issues
show
Bug introduced by
It seems like $this->projectRootNamespace can also be of type null; however, parameter $projectRootNamespace of EdmondsCommerce\Doctrine...tProjectRootNamespace() does only seem to accept string, 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

178
        $replaceTemplateNamespace->setProjectRootNamespace(/** @scrutinizer ignore-type */ $this->projectRootNamespace);
Loading history...
179
        $this->pipeline->register($replaceTemplateNamespace);
180
    }
181
182
    public function getTargetFile(): File
183
    {
184
        return $this->targetFile;
185
    }
186
187
    /**
188
     * Write the file only if it doesn't already exist
189
     *
190
     * @return string
191
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
192
     */
193
    public function writeIfNotExists(): string
194
    {
195
        if ($this->targetFile->exists()) {
196
            return $this->targetFile->getPath();
197
        }
198
199
        return $this->write();
200
    }
201
202
    /**
203
     * Write the file and return the generated path
204
     *
205
     * @return string
206
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
207
     */
208
    public function write(): string
209
    {
210
        $this->targetFile->removeIfExists();
211
212
        return $this->fileWriter->write($this->targetFile);
213
    }
214
215
    protected function registerEntityReplaceName(string $entityFqn): void
216
    {
217
        $replaceName = new ReplaceNameProcess();
218
        $replaceName->setArgs(self::TEMPLATE_ENTITY_NAME, $this->namespaceHelper->basename($entityFqn));
219
        $this->pipeline->register($replaceName);
220
    }
221
}
222