From 7ad74ae03af44ad4b653ae9cc74fe89c8bb3ad46 Mon Sep 17 00:00:00 2001 From: Olivier PEREZ Date: Thu, 2 Apr 2015 23:10:41 +0200 Subject: [PATCH] Create PollRepository + Extract creation poll code from Service to Repository --- .../Framadate/Repositories/PollRepository.php | 69 +++++++++++++++++++ .../Repositories/RepositoryFactory.php | 46 +++++++++++++ .../Framadate/Services/PollService.php | 44 +++--------- app/inc/init.php | 2 + 4 files changed, 125 insertions(+), 36 deletions(-) create mode 100644 app/classes/Framadate/Repositories/PollRepository.php create mode 100644 app/classes/Framadate/Repositories/RepositoryFactory.php diff --git a/app/classes/Framadate/Repositories/PollRepository.php b/app/classes/Framadate/Repositories/PollRepository.php new file mode 100644 index 0000000..fecc4e3 --- /dev/null +++ b/app/classes/Framadate/Repositories/PollRepository.php @@ -0,0 +1,69 @@ +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)); + } + + } + } + +} diff --git a/app/classes/Framadate/Repositories/RepositoryFactory.php b/app/classes/Framadate/Repositories/RepositoryFactory.php new file mode 100644 index 0000000..f52ba8c --- /dev/null +++ b/app/classes/Framadate/Repositories/RepositoryFactory.php @@ -0,0 +1,46 @@ +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) { diff --git a/app/inc/init.php b/app/inc/init.php index 9e17515..63379ef 100644 --- a/app/inc/init.php +++ b/app/inc/init.php @@ -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;