EntityManager::create()   C
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 5
nop 3
1
<?php
2
3
namespace steevanb\DoctrineEvents\Doctrine\ORM;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\ORM\Configuration;
8
use Doctrine\ORM\EntityManager as DoctrineEntityManager;
9
use Doctrine\ORM\ORMException;
10
use steevanb\DoctrineEvents\Behavior\ReflectionTrait;
11
12
class EntityManager extends DoctrineEntityManager
13
{
14
    use ReflectionTrait;
15
16
    /**
17
     * Copied from Doctrine\ORM\EntityManager, cause return use new EntityManager() instead of new static()
18
     *
19
     * @param mixed $conn
20
     * @param Configuration $config
21
     * @param EventManager|null $eventManager
22
     * @return $this
23
     * @throws ORMException
24
     * @throws \InvalidArgumentException
25
     */
26
    public static function create($conn, Configuration $config, EventManager $eventManager = null)
27
    {
28
        if ( ! $config->getMetadataDriverImpl()) {
29
            throw ORMException::missingMappingDriverImpl();
30
        }
31
32
        switch (true) {
33
            case (is_array($conn)):
34
                $conn = \Doctrine\DBAL\DriverManager::getConnection(
35
                    $conn, $config, ($eventManager ?: new EventManager())
36
                );
37
                break;
38
39
            case ($conn instanceof Connection):
40
                if ($eventManager !== null && $conn->getEventManager() !== $eventManager) {
41
                    throw ORMException::mismatchedEventManager();
42
                }
43
                break;
44
45
            default:
46
                throw new \InvalidArgumentException("Invalid argument: " . $conn);
47
        }
48
49
        return new static($conn, $config, $conn->getEventManager());
50
    }
51
52
    /**
53
     * @param Connection $conn
54
     * @param Configuration $config
55
     * @param EventManager $eventManager
56
     */
57
    protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager)
58
    {
59
        parent::__construct($conn, $config, $eventManager);
60
61
        $this->setParentPrivatePropertyValue('unitOfWork', new UnitOfWork($this));
62
    }
63
}
64