Create PollRepository + Extract creation poll code from Service to Repository
This commit is contained in:
parent
8f4c5122c3
commit
7ad74ae03a
69
app/classes/Framadate/Repositories/PollRepository.php
Normal file
69
app/classes/Framadate/Repositories/PollRepository.php
Normal 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
46
app/classes/Framadate/Repositories/RepositoryFactory.php
Normal file
46
app/classes/Framadate/Repositories/RepositoryFactory.php
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -20,16 +20,19 @@ namespace Framadate\Services;
|
|||||||
|
|
||||||
use Framadate\Form;
|
use Framadate\Form;
|
||||||
use Framadate\FramaDB;
|
use Framadate\FramaDB;
|
||||||
|
use Framadate\Repositories\RepositoryFactory;
|
||||||
use Framadate\Utils;
|
use Framadate\Utils;
|
||||||
|
|
||||||
class PollService {
|
class PollService {
|
||||||
|
|
||||||
private $connect;
|
private $connect;
|
||||||
private $logService;
|
private $logService;
|
||||||
|
private $pollRepository;
|
||||||
|
|
||||||
function __construct(FramaDB $connect, LogService $logService) {
|
function __construct(FramaDB $connect, LogService $logService) {
|
||||||
$this->connect = $connect;
|
$this->connect = $connect;
|
||||||
$this->logService = $logService;
|
$this->logService = $logService;
|
||||||
|
$this->pollRepository = RepositoryFactory::pollRepository();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -136,46 +139,15 @@ class PollService {
|
|||||||
$admin_poll_id = $poll_id . $this->random(8);
|
$admin_poll_id = $poll_id . $this->random(8);
|
||||||
|
|
||||||
// Insert poll + slots
|
// Insert poll + slots
|
||||||
$this->connect->beginTransaction();
|
$this->pollRepository->beginTransaction();
|
||||||
|
$this->pollRepository->insertPoll($poll_id, $admin_poll_id, $form);
|
||||||
// TODO Extract this to FramaDB (or repository layer)
|
$this->pollRepository->insertSlots($poll_id, $form->getChoices());
|
||||||
$sql = 'INSERT INTO ' . Utils::table('poll') . '
|
$this->pollRepository->commit();
|
||||||
(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->logService->log('CREATE_POLL', 'id:' . $poll_id . ', title: ' . $form->title . ', format:' . $form->format . ', admin:' . $form->admin_name . ', mail:' . $form->admin_mail);
|
$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) {
|
private function random($car) {
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
|
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
|
||||||
*/
|
*/
|
||||||
use Framadate\FramaDB;
|
use Framadate\FramaDB;
|
||||||
|
use Framadate\Repositories\RepositoryFactory;
|
||||||
|
|
||||||
// Autoloading of dependencies with Composer
|
// Autoloading of dependencies with Composer
|
||||||
require_once __DIR__ . '/../../vendor/autoload.php';
|
require_once __DIR__ . '/../../vendor/autoload.php';
|
||||||
@ -41,4 +42,5 @@ require_once __DIR__ . '/smarty.php';
|
|||||||
|
|
||||||
// Connection to database
|
// Connection to database
|
||||||
$connect = new FramaDB(DB_CONNECTION_STRING, DB_USER, DB_PASSWORD);
|
$connect = new FramaDB(DB_CONNECTION_STRING, DB_USER, DB_PASSWORD);
|
||||||
|
RepositoryFactory::init($connect);
|
||||||
$err = 0;
|
$err = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user