|
|
|
<?php
|
|
|
|
|
|
|
|
namespace common\modules\blog\services;
|
|
|
|
|
|
|
|
use common\modules\blog\entities\BlogCategory;
|
|
|
|
use common\modules\blog\forms\BlogCategoryForm;
|
|
|
|
use common\modules\blog\repositories\BlogCategoryRepository;
|
|
|
|
use common\modules\blog\repositories\BlogRepository;
|
|
|
|
use core\entities\Meta;
|
|
|
|
|
|
|
|
class BlogCategoryManageService
|
|
|
|
{
|
|
|
|
private $categories;
|
|
|
|
private $posts;
|
|
|
|
|
|
|
|
public function __construct(BlogCategoryRepository $categories, BlogRepository $posts)
|
|
|
|
{
|
|
|
|
$this->categories = $categories;
|
|
|
|
$this->posts = $posts;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create(BlogCategoryForm $form): BlogCategory
|
|
|
|
{
|
|
|
|
$category = BlogCategory::create(
|
|
|
|
$form,
|
|
|
|
$form->slug,
|
|
|
|
$form->sort
|
|
|
|
);
|
|
|
|
$this->categories->save($category);
|
|
|
|
return $category;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit($id, BlogCategoryForm $form): void
|
|
|
|
{
|
|
|
|
$category = $this->categories->get($id);
|
|
|
|
$category->edit(
|
|
|
|
$form,
|
|
|
|
$form->slug,
|
|
|
|
$form->sort
|
|
|
|
);
|
|
|
|
$this->categories->save($category);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function remove($id): void
|
|
|
|
{
|
|
|
|
$category = $this->categories->get($id);
|
|
|
|
if ($this->posts->existsByCategory($category->id)) {
|
|
|
|
throw new \DomainException('Unable to remove category with posts.');
|
|
|
|
}
|
|
|
|
$this->categories->remove($category);
|
|
|
|
}
|
|
|
|
}
|