date.chapril.org-framadate/app/classes/Framadate/Services/PurgeService.php
Thomas Citharel 006a191544
Make Framadate compatible with PostgreSQL and great again !
* Move the database handling to Doctrine DBAL
* Move Migrations to Doctrine Migrations
* Rename migrations for Doctrine Migrations Uses
* Fix Migrations
* Change config parameters, introduce db name, host and port parameters and get rid of database url
* Change install form for this
* Add a CLI command to make migrations
* Add config.test.php to be used with APP_ENV=test for testing

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

CS

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Add sqlite to CI and execute migration in test env

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Typo

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

SQLite is already inside the image...

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Rebase two new migrations

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Move from trait to abstract class and remove legacy migration table after checks

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

CS

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Move doctrine command path inside CI

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Move abstract migration class to correct namespace and remove unused command

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

CS

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Check for legacy migration table existence

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Check if legacy migration table exists before deleting it

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Add messages for skipped migrations and fix an issue with MySQL ERR_NO_DATE Migration

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2018-04-22 18:37:53 +02:00

115 lines
3.6 KiB
PHP

<?php
namespace Framadate\Services;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Framadate\Repositories\RepositoryFactory;
/**
* This service helps to purge data.
*
* @package Framadate\Services
*/
class PurgeService {
private $logService;
private $pollRepository;
private $slotRepository;
private $voteRepository;
private $commentRepository;
function __construct(Connection $connect, LogService $logService) {
$this->logService = $logService;
$this->pollRepository = RepositoryFactory::pollRepository();
$this->slotRepository = RepositoryFactory::slotRepository();
$this->voteRepository = RepositoryFactory::voteRepository();
$this->commentRepository = RepositoryFactory::commentRepository();
}
public function repeatedCleanings() {
$this->purgeOldPolls();
if (0 === time() % 10) {
$this->cleanDemoPoll();
}
}
/**
* This methode purges all old polls (the ones with end_date in past).
*
* @return bool true is action succeeded
*/
public function purgeOldPolls() {
try {
$oldPolls = $this->pollRepository->findOldPolls();
$count = count($oldPolls);
if ($count > 0) {
$this->logService->log('EXPIRATION', 'Going to purge ' . $count . ' poll(s)...');
foreach ($oldPolls as $poll) {
if ($this->purgePollById($poll->id)) {
$this->logService->log(
'EXPIRATION_SUCCESS',
'id: ' . $poll->id . ', title:' . $poll->title . ', format: ' . $poll->format . ', admin: ' . $poll->admin_name
);
} else {
$this->logService->log(
'EXPIRATION_FAILED',
'id: ' . $poll->id . ', title:' . $poll->title . ', format: ' . $poll->format . ', admin: ' . $poll->admin_name
);
}
}
}
return $count;
} catch (DBALException $e) {
$this->logService->log('ERROR', $e->getMessage());
return false;
}
}
public function cleanDemoPoll() {
if (!defined("DEMO_POLL_ID") || !defined("DEMO_POLL_NUMBER_VOTES")) {
return;
}
$this->voteRepository->beginTransaction();
$demoVotes = $this->voteRepository->allUserVotesByPollId(DEMO_POLL_ID);
$votesToDelete = count($demoVotes) - DEMO_POLL_NUMBER_VOTES;
if ($votesToDelete > 0) {
$this->voteRepository->deleteOldVotesByPollId(DEMO_POLL_ID, $votesToDelete);
}
$this->voteRepository->commit();
}
/**
* This methode delete all data about a poll.
*
* @param $poll_id int The ID of the poll
* @return bool true is action succeeded
*/
private function purgePollById($poll_id) {
$done = true;
try {
$this->pollRepository->beginTransaction();
$done &= $this->commentRepository->deleteByPollId($poll_id);
$done &= $this->voteRepository->deleteByPollId($poll_id);
$done &= $this->slotRepository->deleteByPollId($poll_id);
$done &= $this->pollRepository->deleteById($poll_id);
if ($done) {
$this->pollRepository->commit();
} else {
$this->pollRepository->rollback();
}
} catch (DBALException $e) {
$this->logService->log('ERROR', $e->getMessage());
}
return $done;
}
}