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.
Passed
Pull Request — master (#154)
by joseph
27:09
created

CliConfigCommandFactory::getMigrationsConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command;
4
5
use Doctrine\DBAL\Migrations\Configuration\Configuration;
6
use Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, EdmondsCommerce\Doctrine...Command\AbstractCommand. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand;
8
use Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand;
9
use Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand;
10
use Doctrine\DBAL\Migrations\Tools\Console\Command\LatestCommand;
11
use Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand;
12
use Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand;
13
use Doctrine\DBAL\Migrations\Tools\Console\Command\UpToDateCommand;
14
use Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand;
15
use Doctrine\ORM\EntityManagerInterface;
16
use EdmondsCommerce\DoctrineStaticMeta\Config;
17
use Psr\Container\ContainerInterface;
18
19
/**
20
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21
 */
22
class CliConfigCommandFactory
23
{
24
    /**
25
     * @var ContainerInterface
26
     */
27
    private $container;
28
    /**
29
     * @var Config
30
     */
31
    private $config;
32
    /**
33
     * @var EntityManagerInterface
34
     */
35
    private $entityManager;
36
    /**
37
     * @var Configuration
38
     */
39
    private $migrationsConfig;
40
41
    public function __construct(ContainerInterface $container, Config $config, EntityManagerInterface $entityManager)
42
    {
43
        $this->container     = $container;
44
        $this->config        = $config;
45
        $this->entityManager = $entityManager;
46
    }
47
48
    /**
49
     * For use in your project's cli-config.php file
50
     *
51
     * @see /cli-config.php
52
     *
53
     * @return array
54
     */
55
    public function getCommands(): array
56
    {
57
        $commands           = [
58
            $this->container->get(GenerateRelationsCommand::class),
59
            $this->container->get(GenerateEntityCommand::class),
60
            $this->container->get(SetRelationCommand::class),
61
            $this->container->get(GenerateFieldCommand::class),
62
            $this->container->get(SetFieldCommand::class),
63
            $this->container->get(SetEmbeddableCommand::class),
64
            $this->container->get(GenerateEmbeddableFromArchetypeCommand::class),
65
            $this->container->get(GenerateEmbeddableSkeletonCommand::class),
66
            $this->container->get(RemoveUnusedRelationsCommand::class),
67
            $this->container->get(OverrideCreateCommand::class),
68
            $this->container->get(OverridesUpdateCommand::class),
69
            $this->container->get(CreateConstraintCommand::class),
70
            $this->container->get(FinaliseBuildCommand::class),
71
            $this->container->get(CreateConstraintCommand::class),
72
        ];
73
        $migrationsCommands = [
74
            $this->container->get(ExecuteCommand::class),
75
            $this->container->get(GenerateCommand::class),
76
            $this->container->get(LatestCommand::class),
77
            $this->container->get(MigrateCommand::class),
78
            $this->container->get(DiffCommand::class),
79
            $this->container->get(UpToDateCommand::class),
80
            $this->container->get(StatusCommand::class),
81
            $this->container->get(VersionCommand::class),
82
        ];
83
        foreach ($migrationsCommands as $command) {
84
            $commands[] = $this->addMigrationsConfig($command);
85
        }
86
87
        return $commands;
88
    }
89
90
    private function addMigrationsConfig(AbstractCommand $command): AbstractCommand
91
    {
92
        $command->setMigrationConfiguration($this->getMigrationsConfig());
93
94
        return $command;
95
    }
96
97
    private function getMigrationsConfig(): Configuration
98
    {
99
        if (null === $this->migrationsConfig) {
100
            $this->migrationsConfig = new Configuration($this->entityManager->getConnection());
101
            $this->migrationsConfig->setMigrationsDirectory($this->config->get(Config::PARAM_MIGRATIONS_DIRECTORY));
102
            $this->migrationsConfig->setMigrationsAreOrganizedByYearAndMonth(true);
103
        }
104
105
        return $this->migrationsConfig;
106
    }
107
108
109
}
110