2014-12-28 23:43:47 +01:00
|
|
|
<?php
|
|
|
|
namespace Framadate\Services;
|
2018-04-18 16:16:22 +02:00
|
|
|
use Doctrine\DBAL\Connection;
|
|
|
|
use Doctrine\DBAL\DBALException;
|
2015-04-03 00:11:36 +02:00
|
|
|
use Framadate\Repositories\RepositoryFactory;
|
2014-12-28 23:43:47 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-29 21:54:07 +01:00
|
|
|
* This service helps to purge data.
|
2014-12-28 23:43:47 +01:00
|
|
|
*
|
|
|
|
* @package Framadate\Services
|
|
|
|
*/
|
|
|
|
class PurgeService {
|
2014-12-29 21:54:07 +01:00
|
|
|
private $logService;
|
2015-04-03 00:11:36 +02:00
|
|
|
private $pollRepository;
|
|
|
|
private $slotRepository;
|
|
|
|
private $voteRepository;
|
|
|
|
private $commentRepository;
|
2014-12-28 23:43:47 +01:00
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
function __construct(Connection $connect, LogService $logService) {
|
2014-12-29 21:54:07 +01:00
|
|
|
$this->logService = $logService;
|
2015-04-03 00:11:36 +02:00
|
|
|
$this->pollRepository = RepositoryFactory::pollRepository();
|
|
|
|
$this->slotRepository = RepositoryFactory::slotRepository();
|
|
|
|
$this->voteRepository = RepositoryFactory::voteRepository();
|
|
|
|
$this->commentRepository = RepositoryFactory::commentRepository();
|
2014-12-28 23:43:47 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 08:48:24 +02:00
|
|
|
public function repeatedCleanings() {
|
|
|
|
$this->purgeOldPolls();
|
2018-04-15 23:30:40 +02:00
|
|
|
|
|
|
|
if (0 === time() % 10) {
|
|
|
|
$this->cleanDemoPoll();
|
|
|
|
}
|
2018-04-14 08:48:24 +02:00
|
|
|
}
|
|
|
|
|
2014-12-28 23:43:47 +01:00
|
|
|
/**
|
|
|
|
* This methode purges all old polls (the ones with end_date in past).
|
|
|
|
*
|
|
|
|
* @return bool true is action succeeded
|
|
|
|
*/
|
2018-04-18 16:16:22 +02:00
|
|
|
public function purgeOldPolls() {
|
|
|
|
try {
|
|
|
|
$oldPolls = $this->pollRepository->findOldPolls();
|
|
|
|
$count = count($oldPolls);
|
2014-12-29 21:54:07 +01:00
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
if ($count > 0) {
|
|
|
|
$this->logService->log('EXPIRATION', 'Going to purge ' . $count . ' poll(s)...');
|
2014-12-29 21:54:07 +01:00
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
2014-12-29 21:54:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
return $count;
|
|
|
|
} catch (DBALException $e) {
|
|
|
|
$this->logService->log('ERROR', $e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-28 23:43:47 +01:00
|
|
|
}
|
2018-04-13 21:24:43 +02:00
|
|
|
|
|
|
|
public function cleanDemoPoll() {
|
2018-04-15 11:59:40 +02:00
|
|
|
if (!defined("DEMO_POLL_ID") || !defined("DEMO_POLL_NUMBER_VOTES")) {
|
2018-04-13 21:24:43 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-12-28 23:43:47 +01:00
|
|
|
/**
|
|
|
|
* This methode delete all data about a poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
|
|
|
* @return bool true is action succeeded
|
|
|
|
*/
|
2018-04-18 16:16:22 +02:00
|
|
|
private function purgePollById($poll_id) {
|
2014-12-30 01:41:25 +01:00
|
|
|
$done = true;
|
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
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);
|
2014-12-30 01:41:25 +01:00
|
|
|
|
2018-04-18 16:16:22 +02:00
|
|
|
if ($done) {
|
|
|
|
$this->pollRepository->commit();
|
|
|
|
} else {
|
|
|
|
$this->pollRepository->rollback();
|
|
|
|
}
|
|
|
|
} catch (DBALException $e) {
|
|
|
|
$this->logService->log('ERROR', $e->getMessage());
|
2014-12-30 01:41:25 +01:00
|
|
|
}
|
2014-12-29 21:54:07 +01:00
|
|
|
|
|
|
|
return $done;
|
2014-12-28 23:43:47 +01:00
|
|
|
}
|
|
|
|
}
|