1
0
mirror of synced 2025-02-10 17:29:27 +03:00

Merge pull request #5860 from peter-gribanov/master

Removed hacky switch/case, migrated to if/else and early return statements
This commit is contained in:
Marco Pivetta 2016-06-08 12:39:03 +02:00
commit 7e4106d47c

View File

@ -22,6 +22,7 @@ namespace Doctrine\ORM;
use Exception; use Exception;
use Doctrine\Common\EventManager; use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\LockMode; use Doctrine\DBAL\LockMode;
use Doctrine\ORM\Query\ResultSetMapping; use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\Proxy\ProxyFactory; use Doctrine\ORM\Proxy\ProxyFactory;
@ -420,14 +421,14 @@ use Doctrine\Common\Util\ClassUtils;
return null; return null;
} }
switch (true) { switch ($lockMode) {
case LockMode::OPTIMISTIC === $lockMode: case LockMode::OPTIMISTIC:
$this->lock($entity, $lockMode, $lockVersion); $this->lock($entity, $lockMode, $lockVersion);
break; break;
case LockMode::NONE === $lockMode: case LockMode::NONE:
case LockMode::PESSIMISTIC_READ === $lockMode: case LockMode::PESSIMISTIC_READ:
case LockMode::PESSIMISTIC_WRITE === $lockMode: case LockMode::PESSIMISTIC_WRITE:
$persister = $unitOfWork->getEntityPersister($class->name); $persister = $unitOfWork->getEntityPersister($class->name);
$persister->refresh($sortedId, $entity, $lockMode); $persister->refresh($sortedId, $entity, $lockMode);
break; break;
@ -438,8 +439,8 @@ use Doctrine\Common\Util\ClassUtils;
$persister = $unitOfWork->getEntityPersister($class->name); $persister = $unitOfWork->getEntityPersister($class->name);
switch (true) { switch ($lockMode) {
case LockMode::OPTIMISTIC === $lockMode: case LockMode::OPTIMISTIC:
if ( ! $class->isVersioned) { if ( ! $class->isVersioned) {
throw OptimisticLockException::notVersioned($class->name); throw OptimisticLockException::notVersioned($class->name);
} }
@ -450,8 +451,8 @@ use Doctrine\Common\Util\ClassUtils;
return $entity; return $entity;
case LockMode::PESSIMISTIC_READ === $lockMode: case LockMode::PESSIMISTIC_READ:
case LockMode::PESSIMISTIC_WRITE === $lockMode: case LockMode::PESSIMISTIC_WRITE:
if ( ! $this->getConnection()->isTransactionActive()) { if ( ! $this->getConnection()->isTransactionActive()) {
throw TransactionRequiredException::transactionRequired(); throw TransactionRequiredException::transactionRequired();
} }
@ -820,7 +821,7 @@ use Doctrine\Common\Util\ClassUtils;
/** /**
* Factory method to create EntityManager instances. * Factory method to create EntityManager instances.
* *
* @param mixed $conn An array with the connection parameters or an existing Connection instance. * @param array|Connection $connection An array with the connection parameters or an existing Connection instance.
* @param Configuration $config The Configuration instance to use. * @param Configuration $config The Configuration instance to use.
* @param EventManager $eventManager The EventManager instance to use. * @param EventManager $eventManager The EventManager instance to use.
* *
@ -829,30 +830,44 @@ use Doctrine\Common\Util\ClassUtils;
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* @throws ORMException * @throws ORMException
*/ */
public static function create($conn, Configuration $config, EventManager $eventManager = null) public static function create($connection, Configuration $config, EventManager $eventManager = null)
{ {
if ( ! $config->getMetadataDriverImpl()) { if ( ! $config->getMetadataDriverImpl()) {
throw ORMException::missingMappingDriverImpl(); throw ORMException::missingMappingDriverImpl();
} }
switch (true) { $connection = static::createConnection($connection, $config, $eventManager);
case (is_array($conn)):
$conn = \Doctrine\DBAL\DriverManager::getConnection(
$conn, $config, ($eventManager ?: new EventManager())
);
break;
case ($conn instanceof Connection): return new EntityManager($connection, $config, $connection->getEventManager());
if ($eventManager !== null && $conn->getEventManager() !== $eventManager) { }
/**
* Factory method to create Connection instances.
*
* @param array|Connection $connection An array with the connection parameters or an existing Connection instance.
* @param Configuration $config The Configuration instance to use.
* @param EventManager $eventManager The EventManager instance to use.
*
* @return Connection
*
* @throws \InvalidArgumentException
* @throws ORMException
*/
protected static function createConnection($connection, Configuration $config, EventManager $eventManager = null)
{
if (is_array($connection)) {
return DriverManager::getConnection($connection, $config, $eventManager ?: new EventManager());
}
if ( ! $connection instanceof Connection) {
throw new \InvalidArgumentException("Invalid argument: " . $connection);
}
if ($eventManager !== null && $connection->getEventManager() !== $eventManager) {
throw ORMException::mismatchedEventManager(); throw ORMException::mismatchedEventManager();
} }
break;
default: return $connection;
throw new \InvalidArgumentException("Invalid argument: " . $conn);
}
return new EntityManager($conn, $config, $conn->getEventManager());
} }
/** /**