szurubooru/src/Services/FavoritesService.php

61 lines
1.6 KiB
PHP
Raw Normal View History

2014-09-27 21:33:31 +02:00
<?php
namespace Szurubooru\Services;
class FavoritesService
{
private $favoritesDao;
2014-09-28 15:21:25 +02:00
private $postScoreDao;
2014-09-27 21:33:31 +02:00
private $userDao;
private $transactionManager;
private $timeService;
2014-09-27 21:33:31 +02:00
public function __construct(
\Szurubooru\Dao\FavoritesDao $favoritesDao,
2014-09-28 15:21:25 +02:00
\Szurubooru\Dao\PostScoreDao $postScoreDao,
2014-09-27 21:33:31 +02:00
\Szurubooru\Dao\UserDao $userDao,
\Szurubooru\Dao\TransactionManager $transactionManager,
\Szurubooru\Services\TimeService $timeService)
2014-09-27 21:33:31 +02:00
{
$this->favoritesDao = $favoritesDao;
2014-09-28 15:21:25 +02:00
$this->postScoreDao = $postScoreDao;
2014-09-27 21:33:31 +02:00
$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)
{
2014-09-28 15:21:25 +02:00
$this->postScoreDao->setScore($user, $post, 1);
return $this->favoritesDao->set($user, $post);
2014-09-27 21:33:31 +02:00
};
return $this->transactionManager->commit($transactionFunc);
}
public function deleteFavorite(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post)
{
$transactionFunc = function() use ($user, $post)
{
2014-09-28 15:21:25 +02:00
$this->favoritesDao->delete($user, $post);
2014-09-27 21:33:31 +02:00
};
2014-09-28 15:21:25 +02:00
$this->transactionManager->commit($transactionFunc);
2014-09-27 21:33:31 +02:00
}
}