Create PollRepository + Extract creation poll code from Service to Repository

This commit is contained in:
Olivier PEREZ 2015-04-02 23:10:41 +02:00
parent 8f4c5122c3
commit 7ad74ae03a
4 changed files with 125 additions and 36 deletions

View File

@ -0,0 +1,69 @@
<?php
namespace Framadate\Repositories;
use Framadate\FramaDB;
use Framadate\Utils;
class PollRepository {
/**
* @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();
}
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->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 (?, ?, ?)');
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));
}
}
}
}

View File

@ -0,0 +1,46 @@
<?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;
use Framadate\FramaDB;
class RepositoryFactory {
private static $connect;
private static $pollRepository;
/**
* @param FramaDB $connect
*/
static function init($connect) {
self::$connect = $connect;
}
/**
* @return PollRepository The singleton of PollRepository
*/
static function pollRepository() {
if (self::$pollRepository == null) {
self::$pollRepository = new PollRepository(self::$connect);
}
return self::$pollRepository;
}
}

View File

@ -20,16 +20,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;
function __construct(FramaDB $connect, LogService $logService) {
$this->connect = $connect;
$this->logService = $logService;
$this->pollRepository = RepositoryFactory::pollRepository();
}
/**
@ -136,46 +139,15 @@ class PollService {
$admin_poll_id = $poll_id . $this->random(8);
// Insert poll + slots
$this->connect->beginTransaction();
// TODO Extract this to FramaDB (or repository layer)
$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->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));
$prepared = $this->connect->prepare('INSERT INTO ' . Utils::table('slot') . ' (poll_id, title, moments) VALUES (?, ?, ?)');
foreach ($form->getChoices() 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));
}
}
$this->connect->commit();
$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 [$poll_id, $admin_poll_id];
return array($poll_id, $admin_poll_id);
}
private function random($car) {

View File

@ -17,6 +17,7 @@
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
use Framadate\FramaDB;
use Framadate\Repositories\RepositoryFactory;
// Autoloading of dependencies with Composer
require_once __DIR__ . '/../../vendor/autoload.php';
@ -41,4 +42,5 @@ require_once __DIR__ . '/smarty.php';
// Connection to database
$connect = new FramaDB(DB_CONNECTION_STRING, DB_USER, DB_PASSWORD);
RepositoryFactory::init($connect);
$err = 0;