2014-10-05 14:54:21 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Dao;
|
|
|
|
|
|
|
|
class ScoreDao extends AbstractDao implements ICrudDao
|
|
|
|
{
|
|
|
|
private $timeService;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
\Szurubooru\DatabaseConnection $databaseConnection,
|
|
|
|
\Szurubooru\Services\TimeService $timeService)
|
|
|
|
{
|
|
|
|
parent::__construct(
|
|
|
|
$databaseConnection,
|
|
|
|
'scores',
|
|
|
|
new \Szurubooru\Dao\EntityConverters\ScoreEntityConverter());
|
|
|
|
|
|
|
|
$this->timeService = $timeService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity)
|
|
|
|
{
|
|
|
|
$query = $this->fpdo->from($this->tableName)->where('userId', $user->getId());
|
|
|
|
|
|
|
|
if ($entity instanceof \Szurubooru\Entities\Post)
|
|
|
|
$query->where('postId', $entity->getId());
|
2014-10-05 16:19:08 +02:00
|
|
|
elseif ($entity instanceof \Szurubooru\Entities\Comment)
|
|
|
|
$query->where('commentId', $entity->getId());
|
2014-10-05 14:54:21 +02:00
|
|
|
else
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
|
|
|
|
$arrayEntities = iterator_to_array($query);
|
|
|
|
$entities = $this->arrayToEntities($arrayEntities);
|
|
|
|
return array_shift($entities);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity, $scoreValue)
|
|
|
|
{
|
|
|
|
$score = $this->getScore($user, $entity);
|
|
|
|
if (!$score)
|
|
|
|
{
|
|
|
|
$score = new \Szurubooru\Entities\Score();
|
|
|
|
$score->setTime($this->timeService->getCurrentTime());
|
2014-10-05 15:15:03 +02:00
|
|
|
$score->setUserId($user->getId());
|
2014-10-05 14:54:21 +02:00
|
|
|
|
|
|
|
if ($entity instanceof \Szurubooru\Entities\Post)
|
2014-10-05 15:15:03 +02:00
|
|
|
$score->setPostId($entity->getId());
|
2014-10-05 16:19:08 +02:00
|
|
|
elseif ($entity instanceof \Szurubooru\Entities\Comment)
|
|
|
|
$score->setCommentId($entity->getId());
|
2014-10-05 14:54:21 +02:00
|
|
|
else
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
$score->setScore($scoreValue);
|
|
|
|
$this->save($score);
|
|
|
|
return $score;
|
|
|
|
}
|
|
|
|
}
|