DoctrineCommand::getEntityManager()   A
last analyzed

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\DBAL\Connection;
6
use Doctrine\DBAL\Sharding\PoolingShardConnection;
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\Tools\EntityGenerator;
9
use Doctrine\Persistence\ManagerRegistry;
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
    /** @var ManagerRegistry */
21
    private $doctrine;
22
23
    public function __construct(ManagerRegistry $doctrine)
24
    {
25
        parent::__construct();
26
27
        $this->doctrine = $doctrine;
28
    }
29
30
    /**
31
     * get a doctrine entity generator
32
     *
33
     * @return EntityGenerator
34
     */
35
    protected function getEntityGenerator()
36
    {
37
        $entityGenerator = new EntityGenerator();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\ORM\Tools\EntityGenerator has been deprecated with message: 2.7 This class is being removed from the ORM and won't have any replacement

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
38
        $entityGenerator->setGenerateAnnotations(false);
39
        $entityGenerator->setGenerateStubMethods(true);
40
        $entityGenerator->setRegenerateEntityIfExists(false);
41
        $entityGenerator->setUpdateEntityIfExists(true);
42
        $entityGenerator->setNumSpaces(4);
43
        $entityGenerator->setAnnotationPrefix('ORM\\');
44
45
        return $entityGenerator;
46
    }
47
48
    /**
49
     * Get a doctrine entity manager by symfony name.
50
     *
51
     * @param string   $name
52
     * @param int|null $shardId
53
     *
54
     * @return EntityManager
55
     */
56
    protected function getEntityManager($name, $shardId = null)
57
    {
58
        $manager = $this->getDoctrine()->getManager($name);
59
60
        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...
61
            if (! $manager->getConnection() instanceof PoolingShardConnection) {
62
                throw new LogicException(sprintf("Connection of EntityManager '%s' must implement shards configuration.", $name));
63
            }
64
65
            $manager->getConnection()->connect($shardId);
66
        }
67
68
        return $manager;
69
    }
70
71
    /**
72
     * Get a doctrine dbal connection by symfony name.
73
     *
74
     * @param string $name
75
     *
76
     * @return Connection
77
     */
78
    protected function getDoctrineConnection($name)
79
    {
80
        return $this->getDoctrine()->getConnection($name);
81
    }
82
83
    /**
84
     * @return ManagerRegistry
85
     */
86
    protected function getDoctrine()
87
    {
88
        return $this->doctrine;
89
    }
90
}
91