2014-12-28 23:43:47 +01:00
|
|
|
<?php
|
|
|
|
namespace Framadate\Services;
|
|
|
|
use Framadate\FramaDB;
|
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
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public function __construct(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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This methode purges all old polls (the ones with end_date in past).
|
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* @return int number of purged polls
|
2014-12-28 23:43:47 +01:00
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function purgeOldPolls(): int
|
|
|
|
{
|
2015-04-03 00:11:36 +02:00
|
|
|
$oldPolls = $this->pollRepository->findOldPolls();
|
2014-12-29 21:54:07 +01:00
|
|
|
$count = count($oldPolls);
|
|
|
|
|
|
|
|
if ($count > 0) {
|
|
|
|
$this->logService->log('EXPIRATION', 'Going to purge ' . $count . ' poll(s)...');
|
|
|
|
|
|
|
|
foreach ($oldPolls as $poll) {
|
2014-12-30 01:41:25 +01:00
|
|
|
if ($this->purgePollById($poll->id)) {
|
2018-02-19 00:18:43 +01:00
|
|
|
$this->logService->log('EXPIRATION_SUCCESS', 'id: ' . $poll->id . ', title:' . $poll->title . ', format: ' . $poll->format . ', admin: ' . $poll->admin_name);
|
2014-12-29 21:54:07 +01:00
|
|
|
} else {
|
2018-02-19 00:18:43 +01:00
|
|
|
$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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-28 19:18:59 +01:00
|
|
|
return $count;
|
2014-12-28 23:43:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This methode delete all data about a poll.
|
|
|
|
*
|
2021-12-20 17:46:50 +01:00
|
|
|
* @param string $poll_id The ID of the poll
|
2014-12-28 23:43:47 +01:00
|
|
|
* @return bool true is action succeeded
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public function purgePollById(string $poll_id): bool
|
|
|
|
{
|
2014-12-30 01:41:25 +01:00
|
|
|
$done = true;
|
|
|
|
|
2015-04-03 00:11:36 +02:00
|
|
|
$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
|
|
|
|
|
|
|
if ($done) {
|
2015-04-03 00:11:36 +02:00
|
|
|
$this->pollRepository->commit();
|
2014-12-30 01:41:25 +01:00
|
|
|
} else {
|
2015-04-03 00:11:36 +02:00
|
|
|
$this->pollRepository->rollback();
|
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
|
|
|
}
|
|
|
|
}
|