szurubooru/src/Services/ScoreService.php

66 lines
1.7 KiB
PHP
Raw Normal View History

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