2014-09-28 15:21:25 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Services;
|
|
|
|
|
2014-10-05 14:54:21 +02:00
|
|
|
class ScoreService
|
2014-09-28 15:21:25 +02:00
|
|
|
{
|
2014-10-05 14:54:21 +02:00
|
|
|
private $scoreDao;
|
2014-09-28 15:21:25 +02:00
|
|
|
private $favoritesDao;
|
|
|
|
private $userDao;
|
|
|
|
private $transactionManager;
|
|
|
|
private $timeService;
|
|
|
|
|
|
|
|
public function __construct(
|
2014-10-05 14:54:21 +02:00
|
|
|
\Szurubooru\Dao\ScoreDao $scoreDao,
|
2014-09-28 15:21:25 +02:00
|
|
|
\Szurubooru\Dao\FavoritesDao $favoritesDao,
|
|
|
|
\Szurubooru\Dao\UserDao $userDao,
|
|
|
|
\Szurubooru\Dao\TransactionManager $transactionManager,
|
|
|
|
\Szurubooru\Services\TimeService $timeService)
|
|
|
|
{
|
2014-10-05 14:54:21 +02:00
|
|
|
$this->scoreDao = $scoreDao;
|
2014-09-28 15:21:25 +02:00
|
|
|
$this->favoritesDao = $favoritesDao;
|
|
|
|
$this->userDao = $userDao;
|
|
|
|
$this->transactionManager = $transactionManager;
|
|
|
|
$this->timeService = $timeService;
|
|
|
|
}
|
|
|
|
|
2014-10-05 14:54:21 +02:00
|
|
|
public function getScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity)
|
2014-09-28 15:21:25 +02:00
|
|
|
{
|
2014-10-05 14:54:21 +02:00
|
|
|
$transactionFunc = function() use ($user, $entity)
|
2014-09-28 15:21:25 +02:00
|
|
|
{
|
2014-10-05 14:54:21 +02:00
|
|
|
return $this->scoreDao->getScore($user, $entity);
|
2014-09-28 15:21:25 +02:00
|
|
|
};
|
|
|
|
return $this->transactionManager->rollback($transactionFunc);
|
|
|
|
}
|
|
|
|
|
2014-10-05 14:54:21 +02:00
|
|
|
public function getScoreValue(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity)
|
2014-09-28 16:56:15 +02:00
|
|
|
{
|
2014-10-05 14:54:21 +02:00
|
|
|
$score = $this->getScore($user, $entity);
|
2014-09-28 16:56:15 +02:00
|
|
|
if (!$score)
|
|
|
|
return 0;
|
|
|
|
return $score->getScore();
|
|
|
|
}
|
|
|
|
|
2014-10-05 14:54:21 +02:00
|
|
|
public function setScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity, $scoreValue)
|
2014-09-28 15:21:25 +02:00
|
|
|
{
|
|
|
|
if ($scoreValue !== 1 and $scoreValue !== 0 and $scoreValue !== -1)
|
|
|
|
throw new \DomainException('Bad score');
|
|
|
|
|
2014-10-05 14:54:21 +02:00
|
|
|
$transactionFunc = function() use ($user, $entity, $scoreValue)
|
2014-09-28 15:21:25 +02:00
|
|
|
{
|
2014-10-05 16:19:08 +02:00
|
|
|
if (($scoreValue !== 1) and ($entity instanceof \Szurubooru\Entities\Post))
|
2014-10-05 14:54:21 +02:00
|
|
|
$this->favoritesDao->delete($user, $entity);
|
2014-09-28 15:21:25 +02:00
|
|
|
|
2014-10-05 14:54:21 +02:00
|
|
|
return $this->scoreDao->setScore($user, $entity, $scoreValue);
|
2014-09-28 15:21:25 +02:00
|
|
|
};
|
|
|
|
return $this->transactionManager->commit($transactionFunc);
|
|
|
|
}
|
|
|
|
}
|