szurubooru/src/Services/FavoritesService.php

65 lines
1.8 KiB
PHP
Raw Normal View History

2014-09-27 21:33:31 +02:00
<?php
namespace Szurubooru\Services;
class FavoritesService
{
private $favoritesDao;
private $userDao;
private $transactionManager;
private $timeService;
2014-09-27 21:33:31 +02:00
public function __construct(
\Szurubooru\Dao\FavoritesDao $favoritesDao,
\Szurubooru\Dao\UserDao $userDao,
\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;
$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);
$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);
}
}