Remove ManipulatorInterface::getRepository method

This commit is contained in:
Romain Neutron
2014-02-27 17:37:57 +01:00
parent 06f29575c7
commit 13e661c4b0
13 changed files with 58 additions and 70 deletions

View File

@@ -15,6 +15,7 @@ use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Authentication\Exception\AccountLockedException;
use Alchemy\Phrasea\Model\Manipulator\UserManipulator;
use Alchemy\Phrasea\Model\Entities\User;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\HttpFoundation\Request;
class NativeAuthentication implements PasswordAuthenticationInterface
@@ -25,12 +26,15 @@ class NativeAuthentication implements PasswordAuthenticationInterface
private $encoder;
/** @var OldPasswordEncoder */
private $oldEncoder;
/** @var EntityRepository */
private $repository;
public function __construct(PasswordEncoder $encoder, OldPasswordEncoder $oldEncoder, UserManipulator $userManipulator)
public function __construct(PasswordEncoder $encoder, OldPasswordEncoder $oldEncoder, UserManipulator $userManipulator, EntityRepository $repo)
{
$this->userManipulator = $userManipulator;
$this->encoder = $encoder;
$this->oldEncoder = $oldEncoder;
$this->repository = $repo;
}
/**
@@ -38,7 +42,7 @@ class NativeAuthentication implements PasswordAuthenticationInterface
*/
public function getUsrId($username, $password, Request $request)
{
if (null === $user = $this->userManipulator->getRepository()->findRealUserByLogin($username)) {
if (null === $user = $this->repository->findRealUserByLogin($username)) {
return null;
}

View File

@@ -107,7 +107,7 @@ class AuthenticationManagerServiceProvider implements ServiceProviderInterface
});
$app['auth.password-checker'] = $app->share(function (Application $app) {
return new NativeAuthentication($app['auth.password-encoder'], $app['auth.old-password-encoder'], $app['manipulator.user']);
return new NativeAuthentication($app['auth.password-encoder'], $app['auth.old-password-encoder'], $app['manipulator.user'], $app['repo.users']);
});
$app['auth.native'] = $app->share(function (Application $app) {

View File

@@ -24,11 +24,11 @@ class ManipulatorServiceProvider implements ServiceProviderInterface
public function register(SilexApplication $app)
{
$app['manipulator.task'] = $app->share(function (SilexApplication $app) {
return new TaskManipulator($app['EM'], $app['task-manager.notifier'], $app['translator']);
return new TaskManipulator($app['EM'], $app['task-manager.notifier'], $app['translator'], $app['repo.tasks']);
});
$app['manipulator.user'] = $app->share(function ($app) {
return new UserManipulator($app['model.user-manager'], $app['auth.password-encoder'], $app['geonames.connector']);
return new UserManipulator($app['model.user-manager'], $app['auth.password-encoder'], $app['geonames.connector'], $app['repo.users']);
});
$app['manipulator.acl'] = $app->share(function ($app) {
@@ -40,7 +40,7 @@ class ManipulatorServiceProvider implements ServiceProviderInterface
});
$app['manipulator.registration'] = $app->share(function ($app) {
return new RegistrationManipulator($app, $app['EM'], $app['acl'], $app['phraseanet.appbox']);
return new RegistrationManipulator($app, $app['EM'], $app['acl'], $app['phraseanet.appbox'], $app['repo.registrations']);
});
}

View File

@@ -29,14 +29,6 @@ class ACLManipulator implements ManipulatorInterface
$this->appbox = $appbox;
}
/**
* @throws LogicException
*/
public function getRepository()
{
throw new LogicException('ACL class is not a doctrine entity and therefore it does not have repository.');
}
/**
* Resets rights for users.
*

View File

@@ -18,10 +18,4 @@ use Doctrine\ORM\EntityRepository;
*/
interface ManipulatorInterface
{
/**
* Returns the entity repository.
*
* @return EntityRepository
*/
public function getRepository();
}

View File

@@ -17,6 +17,7 @@ use Alchemy\Phrasea\Model\Entities\Registration;
use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Model\Repositories\RegistrationRepository;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
class RegistrationManipulator implements ManipulatorInterface
{
@@ -26,13 +27,13 @@ class RegistrationManipulator implements ManipulatorInterface
private $repository;
private $aclProvider;
public function __construct(Application $app, EntityManager $em, ACLProvider $aclProvider, \appbox $appbox)
public function __construct(Application $app, EntityManager $em, ACLProvider $aclProvider, \appbox $appbox, EntityRepository $repo)
{
$this->app = $app;
$this->em = $em;
$this->appbox = $appbox;
$this->aclProvider = $aclProvider;
$this->repository = $this->em->getRepository('Phraseanet:Registration');
$this->repository = $repo;
}
/**
@@ -92,16 +93,6 @@ class RegistrationManipulator implements ManipulatorInterface
$this->em->flush();
}
/**
* Gets Registration Repository.
*
* @return RegistrationRepository
*/
public function getRepository()
{
return $this->repository;
}
/**
* Deletes registration for given user.
*
@@ -112,7 +103,7 @@ class RegistrationManipulator implements ManipulatorInterface
*/
public function deleteUserRegistrations(User $user, array $collections)
{
$qb = $this->getRepository()->createQueryBuilder('d');
$qb = $this->repository->createQueryBuilder('d');
$qb->delete('Phraseanet:Registration', 'd');
$qb->where($qb->expr()->eq('d.user', ':user'));
$qb->setParameter(':user', $user->getId());
@@ -132,7 +123,7 @@ class RegistrationManipulator implements ManipulatorInterface
*/
public function deleteOldRegistrations()
{
$qb = $this->getRepository()->createQueryBuilder('d');
$qb = $this->repository->createQueryBuilder('d');
$qb->delete('Phraseanet:Registration', 'd');
$qb->where($qb->expr()->lt('d.created', ':date'));
$qb->setParameter(':date', new \DateTime('-1 month'));
@@ -146,7 +137,7 @@ class RegistrationManipulator implements ManipulatorInterface
*/
public function deleteRegistrationsOnCollection(\collection $collection)
{
$qb = $this->getRepository()->createQueryBuilder('d');
$qb = $this->repository->createQueryBuilder('d');
$qb->delete('Phraseanet:Registration', 'd');
$qb->where($qb->expr()->eq('d.baseId', ':base'));
$qb->setParameter(':base', $collection->get_base_id());

View File

@@ -16,6 +16,7 @@ use Alchemy\Phrasea\Model\Entities\Task;
use Alchemy\Phrasea\TaskManager\Job\EmptyCollectionJob;
use Alchemy\Phrasea\TaskManager\Notifier;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Translation\TranslatorInterface;
class TaskManipulator implements ManipulatorInterface
@@ -26,12 +27,15 @@ class TaskManipulator implements ManipulatorInterface
private $om;
/** @var TranslatorInterface */
private $translator;
/** @var EntityRepository */
private $repository;
public function __construct(ObjectManager $om, Notifier $notifier, TranslatorInterface $translator)
public function __construct(ObjectManager $om, Notifier $notifier, TranslatorInterface $translator, EntityRepository $repo)
{
$this->om = $om;
$this->notifier = $notifier;
$this->translator = $translator;
$this->repository = $repo;
}
/**

View File

@@ -20,6 +20,7 @@ use Alchemy\Phrasea\Model\Manager\UserManager;
use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Exception\RuntimeException;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
/**
@@ -33,20 +34,15 @@ class UserManipulator implements ManipulatorInterface
private $manager;
/** @var GeonamesConnector */
private $geonamesConnector;
/** @var EntityRepository */
private $repository;
public function __construct(UserManager $manager, PasswordEncoderInterface $passwordEncoder, GeonamesConnector $connector)
public function __construct(UserManager $manager, PasswordEncoderInterface $passwordEncoder, GeonamesConnector $connector, EntityRepository $repo)
{
$this->manager = $manager;
$this->passwordEncoder = $passwordEncoder;
$this->geonamesConnector = $connector;
}
/**
* @{inheritdoc}
*/
public function getRepository()
{
return $this->manager->getObjectManager()->getRepository('Phraseanet:User');
$this->repository = $repo;
}
/**
@@ -278,7 +274,7 @@ class UserManipulator implements ManipulatorInterface
*/
private function doSetLogin(User $user, $login)
{
if (null !== $this->getRepository()->findByLogin($login)) {
if (null !== $this->repository->findByLogin($login)) {
throw new RuntimeException(sprintf('User with login %s already exists.', $login));
}
@@ -300,7 +296,7 @@ class UserManipulator implements ManipulatorInterface
throw new InvalidArgumentException(sprintf('Email %s is not legal.', $email));
}
if (null !== $this->getRepository()->findByEmail($email)) {
if (null !== $this->repository->findByEmail($email)) {
throw new RuntimeException(sprintf('User with email %s already exists.', $email));
}

View File

@@ -19,7 +19,7 @@ class NativeAuthenticationTest extends \PhraseanetTestCase
$manipulator = $this->getUserManipulatorMock($specialUser);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator, $this->createEntityRepositoryMock());
$this->assertNull($auth->getUsrId('a_login', 'a_password', $request));
}
@@ -30,7 +30,7 @@ class NativeAuthenticationTest extends \PhraseanetTestCase
$request = $this->getRequestMock();
$manipulator = $this->getUserManipulatorMock(null);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator, $this->createEntityRepositoryMock());
$this->assertNull($auth->getUsrId('a_login', 'a_password', $request));
}
@@ -45,7 +45,7 @@ class NativeAuthenticationTest extends \PhraseanetTestCase
$manipulator = $this->getUserManipulatorMock($mailLockedUser);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator, $this->createEntityRepositoryMock());
try {
$auth->getUsrId('a_login', 'a_password', $request);
@@ -85,7 +85,7 @@ class NativeAuthenticationTest extends \PhraseanetTestCase
->with($this->equalTo($encoded), $this->equalTo($password), $this->equalTo($nonce))
->will($this->returnValue(true));
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator, $this->createEntityRepositoryMock());
$this->assertEquals($userId, $auth->getUsrId('a_login', $password, $request));
}
@@ -120,7 +120,7 @@ class NativeAuthenticationTest extends \PhraseanetTestCase
->with($this->equalTo($encoded), $this->equalTo($password), $this->equalTo($nonce))
->will($this->returnValue(false));
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator, $this->createEntityRepositoryMock());
$this->assertEquals(false, $auth->getUsrId('a_login', $password, $request));
}
@@ -157,7 +157,7 @@ class NativeAuthenticationTest extends \PhraseanetTestCase
->with($this->equalTo($encoded), $this->equalTo($password), $this->equalTo($nonce))
->will($this->returnValue(false));
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator, $this->createEntityRepositoryMock());
$this->assertEquals(false, $auth->getUsrId('a_login', $password, $request));
}
@@ -203,7 +203,7 @@ class NativeAuthenticationTest extends \PhraseanetTestCase
return true;
}));
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator, $this->createEntityRepositoryMock());
$this->assertEquals($userId, $auth->getUsrId('a_login', $password, $request));
}

View File

@@ -9,7 +9,7 @@ class RegistrationManipulatorTest extends \PhraseanetTestCase
{
public function testCreateRegistration()
{
$service = new RegistrationManipulator(self::$DI['app'], self::$DI['app']['EM'], self::$DI['app']['acl'], self::$DI['app']['phraseanet.appbox']);
$service = new RegistrationManipulator(self::$DI['app'], self::$DI['app']['EM'], self::$DI['app']['acl'], self::$DI['app']['phraseanet.appbox'], self::$DI['app']['repo.registrations']);
$registration = $service->createRegistration(self::$DI['user'], self::$DI['collection']);
$this->assertInstanceOf('Alchemy\Phrasea\Model\Entities\Registration', $registration);
@@ -21,7 +21,7 @@ class RegistrationManipulatorTest extends \PhraseanetTestCase
{
$registration = self::$DI['registration_1'];
$service = new RegistrationManipulator(self::$DI['app'], self::$DI['app']['EM'], self::$DI['app']['acl'], self::$DI['app']['phraseanet.appbox']);
$service = new RegistrationManipulator(self::$DI['app'], self::$DI['app']['EM'], self::$DI['app']['acl'], self::$DI['app']['phraseanet.appbox'], self::$DI['app']['repo.registrations']);
$service->rejectRegistration($registration);
$this->assertFalse($registration->isPending());
@@ -48,13 +48,13 @@ class RegistrationManipulatorTest extends \PhraseanetTestCase
self::$DI['app']['acl'] = $aclProviderMock;
$service = new RegistrationManipulator(self::$DI['app'], self::$DI['app']['EM'], self::$DI['app']['acl'], self::$DI['app']['phraseanet.appbox']);
$service = new RegistrationManipulator(self::$DI['app'], self::$DI['app']['EM'], self::$DI['app']['acl'], self::$DI['app']['phraseanet.appbox'], self::$DI['app']['repo.registrations']);
$service->acceptRegistration($registration, true, false);
}
public function testDeleteRegistrationForUser()
{
$service = new RegistrationManipulator(self::$DI['app'], self::$DI['app']['EM'], self::$DI['app']['acl'], self::$DI['app']['phraseanet.appbox']);
$service = new RegistrationManipulator(self::$DI['app'], self::$DI['app']['EM'], self::$DI['app']['acl'], self::$DI['app']['phraseanet.appbox'], self::$DI['app']['repo.registrations']);
$qb = $service->getRepository()->createQueryBuilder('r');
$nbRegistrationBefore = $qb->select('COUNT(r)')
->where($qb->expr()->eq('r.user', ':user'))
@@ -68,7 +68,7 @@ class RegistrationManipulatorTest extends \PhraseanetTestCase
public function testDeleteOldRegistrations()
{
$service = new RegistrationManipulator(self::$DI['app'], self::$DI['app']['EM'], self::$DI['app']['acl'], self::$DI['app']['phraseanet.appbox']);
$service = new RegistrationManipulator(self::$DI['app'], self::$DI['app']['EM'], self::$DI['app']['acl'], self::$DI['app']['phraseanet.appbox'], self::$DI['app']['repo.registrations']);
$qb = $service->getRepository()->createQueryBuilder('r');
$nbRegistrationBefore = $qb->select('COUNT(r)')->getQuery()->getSingleScalarResult();
$service->deleteOldRegistrations();
@@ -78,7 +78,7 @@ class RegistrationManipulatorTest extends \PhraseanetTestCase
public function testDeleteRegistrationOnCollection()
{
$service = new RegistrationManipulator(self::$DI['app'], self::$DI['app']['EM'], self::$DI['app']['acl'], self::$DI['app']['phraseanet.appbox']);
$service = new RegistrationManipulator(self::$DI['app'], self::$DI['app']['EM'], self::$DI['app']['acl'], self::$DI['app']['phraseanet.appbox'], self::$DI['app']['repo.registrations']);
$qb = $service->getRepository()->createQueryBuilder('r');
$nbRegistrationBefore = $qb->select('COUNT(r)')->getQuery()->getSingleScalarResult();
$service->deleteRegistrationsOnCollection(self::$DI['collection']);

View File

@@ -15,7 +15,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
->method('notify')
->with(Notifier::MESSAGE_CREATE);
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $notifier, self::$DI['app']['translator']);
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $notifier, self::$DI['app']['translator'], self::$DI['app']['repo.tasks']);
$this->assertCount(2, $this->findAllTasks());
$task = $manipulator->create('prout', 'bla bla', 'super settings', 0);
$this->assertEquals('prout', $task->getName());
@@ -36,7 +36,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
->method('notify')
->with(Notifier::MESSAGE_UPDATE);
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $notifier, self::$DI['app']['translator']);
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $notifier, self::$DI['app']['translator'], self::$DI['app']['repo.tasks']);
$task = $this->loadTask();
$task->setName('new name');
$this->assertSame($task, $manipulator->update($task));
@@ -52,7 +52,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
->method('notify')
->with(Notifier::MESSAGE_DELETE);
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $notifier, self::$DI['app']['translator']);
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $notifier, self::$DI['app']['translator'], self::$DI['app']['repo.tasks']);
$task = $this->loadTask();
$manipulator->delete($task);
$this->assertNotContains($task, $this->findAllTasks());
@@ -65,7 +65,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
->method('notify')
->with(Notifier::MESSAGE_UPDATE);
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $notifier, self::$DI['app']['translator']);
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $notifier, self::$DI['app']['translator'], self::$DI['app']['repo.tasks']);
$task = $this->loadTask();
$task->setStatus(Task::STATUS_STOPPED);
self::$DI['app']['EM']->persist($task);
@@ -81,7 +81,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
->method('notify')
->with(Notifier::MESSAGE_UPDATE);
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $notifier, self::$DI['app']['translator']);
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $notifier, self::$DI['app']['translator'], self::$DI['app']['repo.tasks']);
$task = $this->loadTask();
$task->setStatus(Task::STATUS_STARTED);
self::$DI['app']['EM']->persist($task);
@@ -97,7 +97,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
->method('notify')
->with(Notifier::MESSAGE_UPDATE);
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $notifier, self::$DI['app']['translator']);
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $notifier, self::$DI['app']['translator'], self::$DI['app']['repo.tasks']);
$task = $this->loadTask();
$task->setCrashed(42);
$manipulator->resetCrashes($task);
@@ -106,7 +106,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
public function testGetRepository()
{
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $this->createNotifierMock(), self::$DI['app']['translator']);
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $this->createNotifierMock(), self::$DI['app']['translator'], self::$DI['app']['repo.tasks']);
$this->assertSame(self::$DI['app']['EM']->getRepository('Phraseanet:Task'), $manipulator->getRepository());
}
@@ -119,7 +119,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
->method('get_base_id')
->will($this->returnValue(42));
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $this->createNotifierMock(), self::$DI['app']['translator']);
$manipulator = new TaskManipulator(self::$DI['app']['EM'], $this->createNotifierMock(), self::$DI['app']['translator'], self::$DI['app']['repo.tasks']);
$task = $manipulator->createEmptyCollectionJob($collection);
$tasks = self::$DI['app']['EM']->getRepository('Phraseanet:Task')->findAll();

View File

@@ -74,7 +74,7 @@ class UserManipulatorTest extends \PhraseanetTestCase
->getMock();
$user = self::$DI['app']['manipulator.user']->createUser('login', 'password');
$manipulator = new UserManipulator($manager, $passwordInterface, $geonamesConnector);
$manipulator = new UserManipulator($manager, $passwordInterface, $geonamesConnector, self::$DI['app']['repo.tasks']);
$manipulator->setGeonameId($user, 4);
$this->assertEquals(4, $user->getGeonameId());
@@ -140,7 +140,7 @@ class UserManipulatorTest extends \PhraseanetTestCase
$passwordInterface = $this->getMockBuilder('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface')
->getMock();
$user = self::$DI['app']['manipulator.user']->createUser('login', 'password');
$manipulator = new UserManipulator($manager, $passwordInterface, $geonamesConnector);
$manipulator = new UserManipulator($manager, $passwordInterface, $geonamesConnector, self::$DI['app']['repo.tasks']);
$this->setExpectedException(
'Alchemy\Phrasea\Exception\InvalidArgumentException',
'Invalid geonameid -1.'

View File

@@ -656,4 +656,11 @@ abstract class PhraseanetTestCase extends WebTestCase
->disableOriginalConstructor()
->getMock();
}
protected function createEntityRepositoryMock()
{
return $this->getMockBuilder('Doctrine\ORM\EntityRepository')
->disableOriginalConstructor()
->getMock();
}
}