You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8 KiB

6 years ago
<?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->name,
$form->slug,
$form->title,
$form->description,
$form->sort,
new Meta(
$form->meta->title,
$form->meta->description,
$form->meta->keywords
)
);
$this->categories->save($category);
return $category;
}
public function edit($id, BlogCategoryForm $form): void
{
$category = $this->categories->get($id);
$category->edit(
$form->name,
$form->slug,
$form->title,
$form->description,
$form->sort,
new Meta(
$form->meta->title,
$form->meta->description,
$form->meta->keywords
)
);
$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);
}
}