Refactor MailChecker to avoid static instance.

This commit is contained in:
Benoît Burnichon
2015-03-06 15:51:42 +01:00
parent b44fb7ed76
commit e84061924b
4 changed files with 53 additions and 19 deletions

View File

@@ -12,7 +12,7 @@
namespace Alchemy\Phrasea\Setup\Version; namespace Alchemy\Phrasea\Setup\Version;
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Core\Version; use Doctrine\DBAL\Driver\Statement;
/** /**
* In version 3.9 the user table have been removed. * In version 3.9 the user table have been removed.
@@ -21,23 +21,42 @@ use Alchemy\Phrasea\Core\Version;
*/ */
class MailChecker class MailChecker
{ {
/** @var \appbox */
private $appbox;
/** @var string */
private $table;
/**
* Constructor
*
* @param \appbox $appbox
* @param string $table
*/
public function __construct(\appbox $appbox, $table = 'usr')
{
$this->appbox = $appbox;
$this->table = $table;
}
/** /**
* Returns users with duplicated emails * Returns users with duplicated emails
* *
* @param \Application $app
* @param string $table The table name where to look
*
* @return array An array of User * @return array An array of User
*/ */
public static function getWrongEmailUsers(Application $app, $table = 'usr') public function getWrongEmailUsers()
{ {
if (version_compare(Version::getNumber(), '3.9', '>')) { if (version_compare($this->appbox->get_version(), '3.9', '>=')) {
return []; return [];
} }
$sql = 'SELECT usr_mail, usr_id, last_conn, usr_login FROM '. $table .' WHERE usr_mail IS NOT NULL'; $builder = $this->appbox->get_connection()->createQueryBuilder();
$stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); /** @var Statement $stmt */
$stmt->execute(); $stmt = $builder
->select('u.usr_mail', 'u.usr_id', 'u.last_conn', 'u.usr_login')
->from($this->table, 'u')
->where($builder->expr()->isNotNull('u.usr_mail'))
->execute()
;
$rs = $stmt->fetchAll(\PDO::FETCH_ASSOC); $rs = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -66,4 +85,14 @@ class MailChecker
return $badUsers; return $badUsers;
} }
/**
* Whether there is users with same emails
*
* @return bool
*/
public function hasWrongEmailUsers()
{
return count($this->getWrongEmailUsers()) > 0;
}
} }

View File

@@ -39,8 +39,8 @@ class Setup_Upgrade
$this->app = $app; $this->app = $app;
if (version_compare($this->app['phraseanet.appbox']->get_version(), '3.9', '<') $checker = new MailChecker($app['phraseanet.appbox']);
&& count(MailChecker::getWrongEmailUsers($app)) > 0) { if ($checker->hasWrongEmailUsers()) {
throw new \Exception_Setup_FixBadEmailAddresses('Please fix the database before starting'); throw new \Exception_Setup_FixBadEmailAddresses('Please fix the database before starting');
} }

View File

@@ -35,15 +35,15 @@ class module_console_systemMailCheck extends Command
{ {
$output->writeln("Processing..."); $output->writeln("Processing...");
$bad_users = []; /** @var appbox $appBox */
if (version_compare($this->getService('phraseanet.appbox')->get_version(), '3.9', '<')) { $appBox = $this->getService('phraseanet.appbox');
$bad_users = MailChecker::getWrongEmailUsers($this->container); $checker = new MailChecker($appBox);
} $bad_users = $checker->getWrongEmailUsers();
foreach ($bad_users as $email => $users) { foreach ($bad_users as $email => $users) {
if ($input->getOption('list')) { if ($input->getOption('list')) {
$this->write_infos($email, $users, $output, $this->getService('phraseanet.appbox')); $this->write_infos($email, $users, $output, $appBox);
} elseif ($this->manage_group($email, $users, $output, $this->getService('phraseanet.appbox')) === false) { } elseif ($this->manage_group($email, $users, $output, $appBox) === false) {
break; break;
} }

View File

@@ -8,8 +8,13 @@ class MailCheckerTest extends \PhraseanetTestCase
{ {
public function testMailChecker() public function testMailChecker()
{ {
$users = MailChecker::getWrongEmailUsers(self::$DI['app'], 'usr_tmp'); $checker = new MailChecker(self::$DI['app']['phraseanet.appbox'], 'usr_tmp');
$this->assertEmpty($checker->getWrongEmailUsers());
}
$this->assertEquals(0, count($users)); public function testItHasNoDuplicateEmailUsers()
{
$checker = new MailChecker(self::$DI['app']['phraseanet.appbox'], 'usr_tmp');
$this->assertFalse($checker->hasWrongEmailUsers());
} }
} }