szurubooru/src/Services/ScoreService.php

58 lines
1.6 KiB
PHP
Raw Normal View History

2014-09-28 15:21:25 +02:00
<?php
namespace Szurubooru\Services;
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(
\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)
{
$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(\Szurubooru\Entities\User $user, \Szurubooru\Entities\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(\Szurubooru\Entities\User $user, \Szurubooru\Entities\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(\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');
$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))
$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);
}
}