Skip to content

Commit

Permalink
Merge pull request PrestaShop#32458 from Hlavtox/simplify-category-im…
Browse files Browse the repository at this point in the history
…ages

Remove menu thumbnails
  • Loading branch information
nicosomb authored Sep 11, 2023
2 parents 58f13dc + b4a03be commit cae2b90
Show file tree
Hide file tree
Showing 42 changed files with 12 additions and 1,248 deletions.
9 changes: 1 addition & 8 deletions src/Adapter/Category/CommandHandler/AddCategoryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
use PrestaShop\PrestaShop\Core\Domain\Category\Command\AddCategoryCommand;
use PrestaShop\PrestaShop\Core\Domain\Category\CommandHandler\AddCategoryHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CannotAddCategoryException;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\MenuThumbnailsLimitException;
use PrestaShop\PrestaShop\Core\Domain\Category\ValueObject\CategoryId;
use PrestaShop\PrestaShop\Core\Domain\Category\ValueObject\MenuThumbnailId;

/**
* Adds new category using legacy object model.
Expand Down Expand Up @@ -64,18 +62,14 @@ public function __construct(CategoryImageUploader $categoryImageUploader)
*/
public function handle(AddCategoryCommand $command)
{
if (count($command->getMenuThumbnailImages()) > count(MenuThumbnailId::ALLOWED_ID_VALUES)) {
throw new MenuThumbnailsLimitException('Maximum number of menu thumbnails exceeded for new category');
}
$category = $this->createCategoryFromCommand($command);

$categoryId = new CategoryId((int) $category->id);

$this->categoryImageUploader->uploadImages(
$categoryId,
$command->getCoverImage(),
$command->getThumbnailImage(),
$command->getMenuThumbnailImages()
$command->getThumbnailImage()
);

return $categoryId;
Expand All @@ -87,7 +81,6 @@ public function handle(AddCategoryCommand $command)
* @return Category
*
* @throws CannotAddCategoryException
* @throws MenuThumbnailsLimitException
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
use PrestaShop\PrestaShop\Core\Domain\Category\CommandHandler\AddRootCategoryHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CannotAddCategoryException;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CategoryException;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\MenuThumbnailsLimitException;
use PrestaShop\PrestaShop\Core\Domain\Category\ValueObject\CategoryId;
use PrestaShop\PrestaShop\Core\Domain\Category\ValueObject\MenuThumbnailId;

/**
* Class AddRootCategoryHandler.
Expand Down Expand Up @@ -79,8 +77,7 @@ public function handle(AddRootCategoryCommand $command)
$this->categoryImageUploader->uploadImages(
$categoryId,
$command->getCoverImage(),
$command->getThumbnailImage(),
$command->getMenuThumbnailImages()
$command->getThumbnailImage()
);

return $categoryId;
Expand All @@ -98,9 +95,6 @@ public function handle(AddRootCategoryCommand $command)
*/
private function createRootCategoryFromCommand(AddRootCategoryCommand $command)
{
if (count($command->getMenuThumbnailImages()) > count(MenuThumbnailId::ALLOWED_ID_VALUES)) {
throw new MenuThumbnailsLimitException('Maximum number of menu thumbnails exceeded for new category');
}
$category = new Category();
$category->is_root_category = true;
$category->level_depth = 1;
Expand Down

This file was deleted.

19 changes: 2 additions & 17 deletions src/Adapter/Category/CommandHandler/EditCategoryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@
use Category;
use PrestaShop\PrestaShop\Adapter\Domain\AbstractObjectModelHandler;
use PrestaShop\PrestaShop\Adapter\Image\Uploader\CategoryImageUploader;
use PrestaShop\PrestaShop\Core\Category\Provider\MenuThumbnailAvailableKeyProvider;
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\Category\Command\EditCategoryCommand;
use PrestaShop\PrestaShop\Core\Domain\Category\CommandHandler\EditCategoryHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CannotEditCategoryException;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CannotEditRootCategoryException;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CategoryNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\MenuThumbnailsLimitException;

/**
* Class EditCategoryHandler.
Expand All @@ -51,17 +49,10 @@ final class EditCategoryHandler extends AbstractObjectModelHandler implements Ed
*/
private $categoryImageUploader;

/**
* @var MenuThumbnailAvailableKeyProvider
*/
private $menuThumbnailAvailableKeyProvider;

public function __construct(
CategoryImageUploader $categoryImageUploader,
MenuThumbnailAvailableKeyProvider $menuThumbnailAvailableKeyProvider
CategoryImageUploader $categoryImageUploader
) {
$this->categoryImageUploader = $categoryImageUploader;
$this->menuThumbnailAvailableKeyProvider = $menuThumbnailAvailableKeyProvider;
}

/**
Expand All @@ -72,11 +63,6 @@ public function __construct(
*/
public function handle(EditCategoryCommand $command)
{
$availableKeys = $this->menuThumbnailAvailableKeyProvider->getAvailableKeys($command->getCategoryId()->getValue());

if (count($command->getMenuThumbnailImages()) > count($availableKeys)) {
throw new MenuThumbnailsLimitException('Maximum number of menu thumbnails exceeded for new category');
}
$category = new Category($command->getCategoryId()->getValue());

if (!$category->id) {
Expand All @@ -92,8 +78,7 @@ public function handle(EditCategoryCommand $command)
$this->categoryImageUploader->uploadImages(
$command->getCategoryId(),
$command->getCoverImage(),
$command->getThumbnailImage(),
$command->getMenuThumbnailImages()
$command->getThumbnailImage()
);
}

Expand Down
21 changes: 2 additions & 19 deletions src/Adapter/Category/CommandHandler/EditRootCategoryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@
use Category;
use PrestaShop\PrestaShop\Adapter\Domain\AbstractObjectModelHandler;
use PrestaShop\PrestaShop\Adapter\Image\Uploader\CategoryImageUploader;
use PrestaShop\PrestaShop\Core\Category\Provider\MenuThumbnailAvailableKeyProvider;
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\Category\Command\EditRootCategoryCommand;
use PrestaShop\PrestaShop\Core\Domain\Category\CommandHandler\EditRootCategoryHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CannotEditCategoryException;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CannotEditRootCategoryException;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CategoryException;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CategoryNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\MenuThumbnailsLimitException;

/**
* Class EditRootCategoryHandler.
Expand All @@ -50,17 +48,10 @@ final class EditRootCategoryHandler extends AbstractObjectModelHandler implement
*/
private $categoryImageUploader;

/**
* @var MenuThumbnailAvailableKeyProvider
*/
private $menuThumbnailAvailableKeyProvider;

public function __construct(
CategoryImageUploader $categoryImageUploader,
MenuThumbnailAvailableKeyProvider $menuThumbnailAvailableKeyProvider
CategoryImageUploader $categoryImageUploader
) {
$this->categoryImageUploader = $categoryImageUploader;
$this->menuThumbnailAvailableKeyProvider = $menuThumbnailAvailableKeyProvider;
}

/**
Expand All @@ -72,16 +63,9 @@ public function __construct(
* @throws CannotEditRootCategoryException
* @throws CategoryException
* @throws CategoryNotFoundException
* @throws MenuThumbnailsLimitException
*/
public function handle(EditRootCategoryCommand $command)
{
$availableKeys = $this->menuThumbnailAvailableKeyProvider->getAvailableKeys($command->getCategoryId()->getValue());

if (count($command->getMenuThumbnailImages()) > count($availableKeys)) {
throw new MenuThumbnailsLimitException('Maximum number of menu thumbnails exceeded for new category');
}

$category = new Category($command->getCategoryId()->getValue());

if (!$category->id) {
Expand All @@ -97,8 +81,7 @@ public function handle(EditRootCategoryCommand $command)
$this->categoryImageUploader->uploadImages(
$command->getCategoryId(),
$command->getCoverImage(),
$command->getThumbnailImage(),
$command->getMenuThumbnailImages()
$command->getThumbnailImage()
);
}

Expand Down
34 changes: 0 additions & 34 deletions src/Adapter/Category/QueryHandler/GetCategoryForEditingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
use PrestaShop\PrestaShop\Core\Domain\Category\QueryHandler\GetCategoryForEditingHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Category\QueryResult\EditableCategory;
use PrestaShop\PrestaShop\Core\Domain\Category\ValueObject\CategoryId;
use PrestaShop\PrestaShop\Core\Domain\Category\ValueObject\MenuThumbnailId;
use PrestaShop\PrestaShop\Core\Image\Parser\ImageTagSourceParserInterface;
use Shop;

Expand Down Expand Up @@ -108,7 +107,6 @@ public function handle(GetCategoryForEditing $query)
(bool) $category->is_root_category,
$this->getCoverImage($query->getCategoryId()),
$this->getThumbnailImage($query->getCategoryId()),
$this->getMenuThumbnailImages($query->getCategoryId()),
$subcategories->fetchAll(PDO::FETCH_COLUMN),
$category->additional_description
);
Expand Down Expand Up @@ -205,36 +203,4 @@ private function getThumbnailImage(CategoryId $categoryId)

return null;
}

/**
* @param CategoryId $categoryId
*
* @return array
*/
private function getMenuThumbnailImages(CategoryId $categoryId)
{
$menuThumbnails = [];

foreach (MenuThumbnailId::ALLOWED_ID_VALUES as $id) {
$thumbnailPath = _PS_CAT_IMG_DIR_ . $categoryId->getValue() . '-' . $id . '_thumb.jpg';

if (file_exists($thumbnailPath)) {
$imageTag = ImageManager::thumbnail(
$thumbnailPath,
'category_' . $categoryId->getValue() . '-' . $id . '_thumb.jpg',
100,
'jpg',
true,
true
);

$menuThumbnails[$id] = [
'path' => $this->imageTagSourceParser->parse($imageTag),
'id' => $id,
];
}
}

return $menuThumbnails;
}
}
19 changes: 2 additions & 17 deletions src/Adapter/Image/Uploader/CategoryImageUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,23 @@ class CategoryImageUploader
*/
private $categoryThumbnailUploader;

/**
* @var ImageUploaderInterface
*/
private $categoryMenuThumbnailUploader;

public function __construct(
ImageUploaderInterface $categoryCoverUploader,
ImageUploaderInterface $categoryThumbnailUploader,
ImageUploaderInterface $categoryMenuThumbnailUploader
ImageUploaderInterface $categoryThumbnailUploader
) {
$this->categoryCoverUploader = $categoryCoverUploader;
$this->categoryThumbnailUploader = $categoryThumbnailUploader;
$this->categoryMenuThumbnailUploader = $categoryMenuThumbnailUploader;
}

/**
* @param CategoryId $categoryId
* @param UploadedFile|null $coverImage
* @param UploadedFile|null $thumbnailImage
* @param UploadedFile[] $menuThumbnailImages
*/
public function uploadImages(
CategoryId $categoryId,
UploadedFile $coverImage = null,
UploadedFile $thumbnailImage = null,
array $menuThumbnailImages = []
UploadedFile $thumbnailImage = null
): void {
if (null !== $coverImage) {
$this->categoryCoverUploader->upload($categoryId->getValue(), $coverImage);
Expand All @@ -79,11 +70,5 @@ public function uploadImages(
if (null !== $thumbnailImage) {
$this->categoryThumbnailUploader->upload($categoryId->getValue(), $thumbnailImage);
}

if (!empty($menuThumbnailImages)) {
foreach ($menuThumbnailImages as $menuThumbnail) {
$this->categoryMenuThumbnailUploader->upload($categoryId->getValue(), $menuThumbnail);
}
}
}
}
Loading

0 comments on commit cae2b90

Please sign in to comment.