Completed
Pull Request — master (#877)
by Kévin
02:06
created

DoctrineCommand::getEntityManager()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.7998
cc 3
nc 3
nop 2
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
13
/**
14
 * Base class for Doctrine console commands to extend from.
15
 *
16
 * @internal
17
 */
18
abstract class DoctrineCommand extends Command
19
{
20
    protected $doctrine;
21
22
    public function __construct(ManagerRegistry $doctrine)
23
    {
24
        parent::__construct();
25
26
        $this->doctrine = $doctrine;
27
    }
28
29
    /**
30
     * get a doctrine entity generator
31
     *
32
     * @return EntityGenerator
33
     */
34
    protected function getEntityGenerator()
35
    {
36
        $entityGenerator = new EntityGenerator();
37
        $entityGenerator->setGenerateAnnotations(false);
38
        $entityGenerator->setGenerateStubMethods(true);
39
        $entityGenerator->setRegenerateEntityIfExists(false);
40
        $entityGenerator->setUpdateEntityIfExists(true);
41
        $entityGenerator->setNumSpaces(4);
42
        $entityGenerator->setAnnotationPrefix('ORM\\');
43
44
        return $entityGenerator;
45
    }
46
47
    /**
48
     * Get a doctrine entity manager by symfony name.
49
     *
50
     * @param string   $name
51
     * @param int|null $shardId
52
     *
53
     * @return EntityManager
54
     */
55
    protected function getEntityManager($name, $shardId = null)
56
    {
57
        $manager = $this->doctrine->getManager($name);
58
59
        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...
60
            if (! $manager->getConnection() instanceof PoolingShardConnection) {
61
                throw new LogicException(sprintf("Connection of EntityManager '%s' must implement shards configuration.", $name));
62
            }
63
64
            $manager->getConnection()->connect($shardId);
65
        }
66
67
        return $manager;
68
    }
69
70
    /**
71
     * Get a doctrine dbal connection by symfony name.
72
     *
73
     * @param string $name
74
     *
75
     * @return Connection
76
     */
77
    protected function getDoctrineConnection($name)
78
    {
79
        return $this->doctrine->getConnection($name);
80
    }
81
}
82