BookStack/app/Entities/Repos/BookRepo.php

83 lines
2.2 KiB
PHP
Raw Permalink Normal View History

2021-06-26 11:23:15 -04:00
<?php
namespace BookStack\Entities\Repos;
2019-09-15 18:28:23 -04:00
2023-05-17 12:56:55 -04:00
use BookStack\Activity\ActivityType;
use BookStack\Activity\TagRepo;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Exceptions\ImageUploadException;
use BookStack\Facades\Activity;
use BookStack\Uploads\ImageRepo;
use Exception;
use Illuminate\Http\UploadedFile;
2019-09-15 18:28:23 -04:00
class BookRepo
2019-09-15 18:28:23 -04:00
{
public function __construct(
protected BaseRepo $baseRepo,
protected TagRepo $tagRepo,
protected ImageRepo $imageRepo,
protected TrashCan $trashCan,
) {
}
2019-09-15 18:28:23 -04:00
/**
2021-06-26 11:23:15 -04:00
* Create a new book in the system.
2019-09-15 18:28:23 -04:00
*/
public function create(array $input): Book
2019-09-15 18:28:23 -04:00
{
$book = new Book();
$this->baseRepo->create($book, $input);
$this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
$this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
Activity::add(ActivityType::BOOK_CREATE, $book);
2021-06-26 11:23:15 -04:00
return $book;
}
2019-09-15 18:28:23 -04:00
/**
* Update the given book.
*/
public function update(Book $book, array $input): Book
{
$this->baseRepo->update($book, $input);
if (array_key_exists('default_template_id', $input)) {
$this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
}
if (array_key_exists('image', $input)) {
$this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
}
Activity::add(ActivityType::BOOK_UPDATE, $book);
2021-06-26 11:23:15 -04:00
return $book;
}
2019-09-15 18:28:23 -04:00
/**
* Update the given book's cover image, or clear it.
2021-06-26 11:23:15 -04:00
*
* @throws ImageUploadException
* @throws Exception
*/
public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
{
$this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
2019-09-15 18:28:23 -04:00
}
/**
* Remove a book from the system.
2021-06-26 11:23:15 -04:00
*
* @throws Exception
*/
public function destroy(Book $book)
{
$this->trashCan->softDestroyBook($book);
Activity::add(ActivityType::BOOK_DELETE, $book);
$this->trashCan->autoClearOld();
}
}