Create SlotRepository + AbstractRepository

This commit is contained in:
Olivier PEREZ 2015-04-02 23:23:34 +02:00
parent 7ad74ae03a
commit 7d84ec1e73
6 changed files with 163 additions and 106 deletions

View File

@ -79,30 +79,6 @@ class FramaDB {
return $this->pdo->query($sql);
}
function findPollById($poll_id) {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('poll') . '` WHERE id = ?');
$prepared->execute(array($poll_id));
$poll = $prepared->fetch();
$prepared->closeCursor();
return $poll;
}
public function existsById($poll_id) {
$prepared = $this->prepare('SELECT 1 FROM `' . Utils::table('poll') . '` WHERE id = ?');
$prepared->execute(array($poll_id));
return $prepared->rowCount() > 0;
}
function updatePoll($poll) {
$prepared = $this->prepare('UPDATE `' . Utils::table('poll') . '` SET title=?, admin_name=?, admin_mail=?, description=?, end_date=?, active=?, editable=? WHERE id = ?');
return $prepared->execute([$poll->title, $poll->admin_name, $poll->admin_mail, $poll->description, $poll->end_date, $poll->active, $poll->editable, $poll->id]);
}
function allCommentsByPollId($poll_id) {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('comment') . '` WHERE poll_id = ? ORDER BY id');
$prepared->execute(array($poll_id));
@ -117,13 +93,6 @@ class FramaDB {
return $prepared->fetchAll();
}
function allSlotsByPollId($poll_id) {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('slot') . '` WHERE poll_id = ? ORDER BY title');
$prepared->execute(array($poll_id));
return $prepared->fetchAll();
}
function insertDefaultVote($poll_id, $insert_position) {
$prepared = $this->prepare('UPDATE `' . Utils::table('vote') . '` SET choices = CONCAT(SUBSTRING(choices, 1, ?), "0", SUBSTRING(choices, ?)) WHERE poll_id = ?');

View File

@ -0,0 +1,33 @@
<?php
namespace Framadate\Repositories;
use Framadate\FramaDB;
abstract class AbstractRepository {
/**
* @var FramaDB
*/
private $connect;
/**
* PollRepository constructor.
* @param FramaDB $connect
*/
function __construct(FramaDB $connect) {
$this->connect = $connect;
}
public function beginTransaction() {
$this->connect->beginTransaction();
}
public function commit() {
$this->connect->commit();
}
public function prepare($sql) {
return $this->connect->prepare($sql);
}
}

View File

@ -1,69 +1,44 @@
<?php
namespace Framadate\Repositories;
use Framadate\FramaDB;
use Framadate\Utils;
class PollRepository {
class PollRepository extends AbstractRepository {
/**
* @var FramaDB
*/
private $connect;
/**
* PollRepository constructor.
* @param FramaDB $connect
*/
function __construct($connect) {
$this->connect = $connect;
}
public function beginTransaction() {
$this->connect->beginTransaction();
}
public function commit() {
$this->connect->commit();
function __construct(FramaDB $connect) {
parent::__construct($connect);
}
public function insertPoll($poll_id, $admin_poll_id, $form) {
$sql = 'INSERT INTO `' . Utils::table('poll') . '`
(id, admin_id, title, description, admin_name, admin_mail, end_date, format, editable, receiveNewVotes, receiveNewComments)
VALUES (?,?,?,?,?,?,FROM_UNIXTIME(?),?,?,?,?)';
$prepared = $this->connect->prepare($sql);
$prepared = $this->prepare($sql);
$prepared->execute(array($poll_id, $admin_poll_id, $form->title, $form->description, $form->admin_name, $form->admin_mail, $form->end_date, $form->format, $form->editable, $form->receiveNewVotes, $form->receiveNewComments));
}
/**
* @param int $poll_id
* @param array $choices
*/
public function insertSlots($poll_id, $choices) {
$prepared = $this->connect->prepare('INSERT INTO ' . Utils::table('slot') . ' (poll_id, title, moments) VALUES (?, ?, ?)');
function findById($poll_id) {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('poll') . '` WHERE id = ?');
foreach ($choices as $choice) {
$prepared->execute(array($poll_id));
$poll = $prepared->fetch();
$prepared->closeCursor();
// We prepared the slots (joined by comas)
$joinedSlots = '';
$first = true;
foreach ($choice->getSlots() as $slot) {
if ($first) {
$joinedSlots = $slot;
$first = false;
} else {
$joinedSlots .= ',' . $slot;
}
}
return $poll;
}
// We execute the insertion
if (empty($joinedSlots)) {
$prepared->execute(array($poll_id, $choice->getName(), null));
} else {
$prepared->execute(array($poll_id, $choice->getName(), $joinedSlots));
}
public function existsById($poll_id) {
$prepared = $this->prepare('SELECT 1 FROM `' . Utils::table('poll') . '` WHERE id = ?');
}
$prepared->execute(array($poll_id));
return $prepared->rowCount() > 0;
}
function update($poll) {
$prepared = $this->prepare('UPDATE `' . Utils::table('poll') . '` SET title=?, admin_name=?, admin_mail=?, description=?, end_date=?, active=?, editable=? WHERE id = ?');
return $prepared->execute([$poll->title, $poll->admin_name, $poll->admin_mail, $poll->description, $poll->end_date, $poll->active, $poll->editable, $poll->id]);
}
}

View File

@ -23,12 +23,14 @@ use Framadate\FramaDB;
class RepositoryFactory {
private static $connect;
private static $pollRepository;
private static $slotRepository;
/**
* @param FramaDB $connect
*/
static function init($connect) {
static function init(FramaDB $connect) {
self::$connect = $connect;
}
@ -43,4 +45,15 @@ class RepositoryFactory {
return self::$pollRepository;
}
/**
* @return SlotRepository The singleton of SlotRepository
*/
static function slotRepository() {
if (self::$slotRepository == null) {
self::$slotRepository = new SlotRepository(self::$connect);
}
return self::$slotRepository;
}
}

View File

@ -0,0 +1,67 @@
<?php
/**
* This software is governed by the CeCILL-B license. If a copy of this license
* is not distributed with this file, you can obtain one at
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
*
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
* Authors of Framadate/OpenSondate: Framasoft (https://github.com/framasoft)
*
* =============================
*
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
*
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
namespace Framadate\Repositories;
class SlotRepository extends AbstractRepository {
function __construct(FramaDB $connect) {
parent::__construct($connect);
}
/**
* Insert a bulk of slots.
*
* @param int $poll_id
* @param array $choices
*/
public function insertSlots($poll_id, $choices) {
$prepared = $this->prepare('INSERT INTO `' . Utils::table('slot') . '` (poll_id, title, moments) VALUES (?, ?, ?)');
foreach ($choices as $choice) {
// We prepared the slots (joined by comas)
$joinedSlots = '';
$first = true;
foreach ($choice->getSlots() as $slot) {
if ($first) {
$joinedSlots = $slot;
$first = false;
} else {
$joinedSlots .= ',' . $slot;
}
}
// We execute the insertion
if (empty($joinedSlots)) {
$prepared->execute(array($poll_id, $choice->getName(), null));
} else {
$prepared->execute(array($poll_id, $choice->getName(), $joinedSlots));
}
}
}
function listByPollId($poll_id) {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('slot') . '` WHERE poll_id = ? ORDER BY title');
$prepared->execute(array($poll_id));
return $prepared->fetchAll();
}
}

View File

@ -21,18 +21,19 @@ namespace Framadate\Services;
use Framadate\Form;
use Framadate\FramaDB;
use Framadate\Repositories\RepositoryFactory;
use Framadate\Utils;
class PollService {
private $connect;
private $logService;
private $pollRepository;
private $slotRepository;
function __construct(FramaDB $connect, LogService $logService) {
$this->connect = $connect;
$this->logService = $logService;
$this->pollRepository = RepositoryFactory::pollRepository();
$this->slotRepository = RepositoryFactory::slotRepository();
}
/**
@ -43,7 +44,7 @@ class PollService {
*/
function findById($poll_id) {
if (preg_match('/^[\w\d]{16}$/i', $poll_id)) {
return $this->connect->findPollById($poll_id);
return $this->pollRepository->findById($poll_id);
}
return null;
@ -58,7 +59,7 @@ class PollService {
}
function allSlotsByPollId($poll_id) {
return $this->connect->allSlotsByPollId($poll_id);
return $this->slotRepository->listByPollId($poll_id);
}
public function updateVote($poll_id, $vote_id, $name, $choices) {
@ -82,6 +83,29 @@ class PollService {
return $this->connect->countVotesByPollId($poll_id);
}
/**
* @param Form $form
* @return string
*/
function createPoll(Form $form) {
// Generate poll IDs, loop while poll ID already exists
do {
$poll_id = $this->random(16);
} while ($this->pollRepository->existsById($poll_id));
$admin_poll_id = $poll_id . $this->random(8);
// Insert poll + slots
$this->pollRepository->beginTransaction();
$this->pollRepository->insertPoll($poll_id, $admin_poll_id, $form);
$this->slotRepository->insertSlots($poll_id, $form->getChoices());
$this->pollRepository->commit();
$this->logService->log('CREATE_POLL', 'id:' . $poll_id . ', title: ' . $form->title . ', format:' . $form->format . ', admin:' . $form->admin_name . ', mail:' . $form->admin_mail);
return array($poll_id, $admin_poll_id);
}
function computeBestChoices($votes) {
$result = [];
foreach ($votes as $vote) {
@ -126,30 +150,6 @@ class PollService {
return $splitted;
}
/**
* @param Form $form
* @return string
*/
function createPoll(Form $form) {
// Generate poll IDs, loop while poll ID already exists
do {
$poll_id = $this->random(16);
} while ($this->connect->existsById($poll_id));
$admin_poll_id = $poll_id . $this->random(8);
// Insert poll + slots
$this->pollRepository->beginTransaction();
$this->pollRepository->insertPoll($poll_id, $admin_poll_id, $form);
$this->pollRepository->insertSlots($poll_id, $form->getChoices());
$this->pollRepository->commit();
$this->logService->log('CREATE_POLL', 'id:' . $poll_id . ', title: ' . $form->title . ', format:' . $form->format . ', admin:' . $form->admin_name . ', mail:' . $form->admin_mail);
return array($poll_id, $admin_poll_id);
}
private function random($car) {
// TODO Better random ?
$string = '';