2014-09-27 21:33:31 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Services;
|
|
|
|
|
|
|
|
class FavoritesService
|
|
|
|
{
|
|
|
|
private $favoritesDao;
|
|
|
|
private $userDao;
|
|
|
|
private $transactionManager;
|
2014-09-28 11:58:07 +02:00
|
|
|
private $timeService;
|
2014-09-27 21:33:31 +02:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
\Szurubooru\Dao\FavoritesDao $favoritesDao,
|
|
|
|
\Szurubooru\Dao\UserDao $userDao,
|
2014-09-28 11:58:07 +02:00
|
|
|
\Szurubooru\Dao\TransactionManager $transactionManager,
|
|
|
|
\Szurubooru\Services\TimeService $timeService)
|
2014-09-27 21:33:31 +02:00
|
|
|
{
|
|
|
|
$this->favoritesDao = $favoritesDao;
|
|
|
|
$this->userDao = $userDao;
|
|
|
|
$this->transactionManager = $transactionManager;
|
2014-09-28 11:58:07 +02:00
|
|
|
$this->timeService = $timeService;
|
2014-09-27 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFavoriteUsers(\Szurubooru\Entities\Post $post)
|
|
|
|
{
|
|
|
|
$transactionFunc = function() use ($post)
|
|
|
|
{
|
|
|
|
$favorites = $this->favoritesDao->findByPost($post);
|
|
|
|
$userIds = [];
|
|
|
|
foreach ($favorites as $favorite)
|
|
|
|
{
|
|
|
|
$userIds[] = $favorite->getUserId();
|
|
|
|
}
|
|
|
|
return $this->userDao->findByIds($userIds);
|
|
|
|
};
|
|
|
|
return $this->transactionManager->rollback($transactionFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addFavorite(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post)
|
|
|
|
{
|
|
|
|
$transactionFunc = function() use ($user, $post)
|
|
|
|
{
|
|
|
|
$favorite = $this->favoritesDao->findByUserAndPost($user, $post);
|
|
|
|
if (!$favorite)
|
|
|
|
{
|
|
|
|
$favorite = new \Szurubooru\Entities\Favorite();
|
|
|
|
$favorite->setUser($user);
|
|
|
|
$favorite->setPost($post);
|
2014-09-28 11:58:07 +02:00
|
|
|
$favorite->setTime($this->timeService->getCurrentTime());
|
2014-09-27 21:33:31 +02:00
|
|
|
$this->favoritesDao->save($favorite);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return $this->transactionManager->commit($transactionFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteFavorite(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post)
|
|
|
|
{
|
|
|
|
$transactionFunc = function() use ($user, $post)
|
|
|
|
{
|
|
|
|
$favorite = $this->favoritesDao->findByUserAndPost($user, $post);
|
|
|
|
$this->favoritesDao->deleteById($favorite->getId());
|
|
|
|
};
|
|
|
|
return $this->transactionManager->commit($transactionFunc);
|
|
|
|
}
|
|
|
|
}
|