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 (#154)
by joseph
33:02
created

CliConfigCommandFactory::getMigrationsConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 7
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\DBAL\Tools\Console as DBALConsole;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
18
use EdmondsCommerce\DoctrineStaticMeta\Config;
19
use Psr\Container\ContainerInterface;
20
use Symfony\Component\Console\Helper\HelperSet;
21
22
/**
23
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
24
 */
25
class CliConfigCommandFactory
26
{
27
    /**
28
     * @var ContainerInterface
29
     */
30
    private $container;
31
    /**
32
     * @var Config
33
     */
34
    private $config;
35
    /**
36
     * @var EntityManagerInterface
37
     */
38
    private $entityManager;
39
    /**
40
     * @var Configuration
41
     */
42
    private $migrationsConfig;
43
44
    public function __construct(ContainerInterface $container, Config $config, EntityManagerInterface $entityManager)
45
    {
46
        $this->container     = $container;
47
        $this->config        = $config;
48
        $this->entityManager = $entityManager;
49
    }
50
51
    /**
52
     * For use in your project's cli-config.php file
53
     *
54
     * @see /cli-config.php
55
     *
56
     * @return array
57
     */
58
    public function getCommands(): array
59
    {
60
        $commands           = [
61
            $this->container->get(GenerateRelationsCommand::class),
62
            $this->container->get(GenerateEntityCommand::class),
63
            $this->container->get(SetRelationCommand::class),
64
            $this->container->get(GenerateFieldCommand::class),
65
            $this->container->get(SetFieldCommand::class),
66
            $this->container->get(SetEmbeddableCommand::class),
67
            $this->container->get(GenerateEmbeddableFromArchetypeCommand::class),
68
            $this->container->get(GenerateEmbeddableSkeletonCommand::class),
69
            $this->container->get(RemoveUnusedRelationsCommand::class),
70
            $this->container->get(OverrideCreateCommand::class),
71
            $this->container->get(OverridesUpdateCommand::class),
72
            $this->container->get(CreateConstraintCommand::class),
73
            $this->container->get(FinaliseBuildCommand::class),
74
            $this->container->get(CreateConstraintCommand::class),
75
        ];
76
        $migrationsCommands = [
77
            $this->container->get(ExecuteCommand::class),
78
            $this->container->get(GenerateCommand::class),
79
            $this->container->get(LatestCommand::class),
80
            $this->container->get(MigrateCommand::class),
81
            $this->container->get(DiffCommand::class),
82
            $this->container->get(UpToDateCommand::class),
83
            $this->container->get(StatusCommand::class),
84
            $this->container->get(VersionCommand::class),
85
        ];
86
        foreach ($migrationsCommands as $command) {
87
            $commands[] = $this->addMigrationsConfig($command);
88
        }
89
90
        return $commands;
91
    }
92
93
    private function addMigrationsConfig(AbstractCommand $command): AbstractCommand
94
    {
95
        $command->setMigrationConfiguration($this->getMigrationsConfig());
96
97
        return $command;
98
    }
99
100
    private function getMigrationsConfig(): Configuration
101
    {
102
        if (null === $this->migrationsConfig) {
103
            $this->migrationsConfig = new Configuration($this->entityManager->getConnection());
104
            $this->migrationsConfig->setMigrationsDirectory($this->config->get(Config::PARAM_MIGRATIONS_DIRECTORY));
105
            $this->migrationsConfig->setMigrationsAreOrganizedByYearAndMonth(true);
106
            $this->migrationsConfig->setMigrationsNamespace('Migrations');
107
        }
108
109
        return $this->migrationsConfig;
110
    }
111
112
    public function createHelperSet(): HelperSet
113
    {
114
        return new HelperSet(
115
            [
116
                'db'       => new DBALConsole\Helper\ConnectionHelper($this->entityManager->getConnection()),
117
                'em'       => new EntityManagerHelper($this->entityManager),
118
                'question' => new \Symfony\Component\Console\Helper\QuestionHelper(),
119
            ]
120
        );
121
    }
122
123
}
124