Completed
Pull Request — master (#877)
by Robin
02:10
created

DoctrineCommand::getContainer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Command;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\Sharding\PoolingShardConnection;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\Tools\EntityGenerator;
10
use LogicException;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
/**
15
 * Base class for Doctrine console commands to extend from.
16
 *
17
 * @internal
18
 */
19
abstract class DoctrineCommand extends Command
20
{
21
    /** @var ManagerRegistry|null */
22
    private $doctrine;
23
24
    /** @var ContainerInterface|null */
25
    private $container;
26
27
    public function __construct(ManagerRegistry $doctrine = null)
28
    {
29
        parent::__construct();
30
31
        if (null === $doctrine) {
32
            @trigger_error(sprintf(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
33
                'The "%s" constructor expects a "%s" instance as first argument, not passing it will throw a \TypeError in DoctrineBundle 2.0.',
34
                get_class($this),
35
                ManagerRegistry::class
36
            ), E_USER_DEPRECATED);
37
        }
38
39
        $this->doctrine = $doctrine;
40
    }
41
42
    /**
43
     * @deprecated
44
     */
45
    public function setContainer(ContainerInterface $container = null)
46
    {
47
        @trigger_error(sprintf('The "%s()" method is deprecated and will be removed in DoctrineBundle 2.0.', __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
48
49
        $this->container = $container;
50
    }
51
52
    /**
53
     * @return ContainerInterface
54
     *
55
     * @throws \LogicException
56
     *
57
     * @deprecated
58
     */
59
    protected function getContainer()
60
    {
61
        @trigger_error(sprintf('The "%s()" method is deprecated and will be removed in DoctrineBundle 2.0.', __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
62
63
        if (null === $this->container) {
64
            $application = $this->getApplication();
65
            if (null === $application) {
66
                throw new \LogicException('The container cannot be retrieved as the application instance is not yet set.');
67
            }
68
69
            $this->container = $application->getKernel()->getContainer();
70
        }
71
72
        return $this->container;
73
    }
74
75
    /**
76
     * get a doctrine entity generator
77
     *
78
     * @return EntityGenerator
79
     */
80
    protected function getEntityGenerator()
81
    {
82
        $entityGenerator = new EntityGenerator();
83
        $entityGenerator->setGenerateAnnotations(false);
84
        $entityGenerator->setGenerateStubMethods(true);
85
        $entityGenerator->setRegenerateEntityIfExists(false);
86
        $entityGenerator->setUpdateEntityIfExists(true);
87
        $entityGenerator->setNumSpaces(4);
88
        $entityGenerator->setAnnotationPrefix('ORM\\');
89
90
        return $entityGenerator;
91
    }
92
93
    /**
94
     * Get a doctrine entity manager by symfony name.
95
     *
96
     * @param string   $name
97
     * @param int|null $shardId
98
     *
99
     * @return EntityManager
100
     */
101
    protected function getEntityManager($name, $shardId = null)
102
    {
103
        $manager = $this->getDoctrine()->getManager($name);
104
105
        if ($shardId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $shardId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
106
            if (! $manager->getConnection() instanceof PoolingShardConnection) {
107
                throw new LogicException(sprintf("Connection of EntityManager '%s' must implement shards configuration.", $name));
108
            }
109
110
            $manager->getConnection()->connect($shardId);
111
        }
112
113
        return $manager;
114
    }
115
116
    /**
117
     * Get a doctrine dbal connection by symfony name.
118
     *
119
     * @param string $name
120
     *
121
     * @return Connection
122
     */
123
    protected function getDoctrineConnection($name)
124
    {
125
        return $this->getDoctrine()->getConnection($name);
126
    }
127
128
    /**
129
     * @return ManagerRegistry
130
     */
131
    protected function getDoctrine()
132
    {
133
        return $this->doctrine ?: $this->doctrine = $this->getContainer()->get('doctrine');
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Bundle\Doctrine...Command::getContainer() has been deprecated.

This method has been deprecated.

Loading history...
134
    }
135
}
136