1<?php 2/** 3 * Copyright since 2007 PrestaShop SA and Contributors 4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA 5 * 6 * NOTICE OF LICENSE 7 * 8 * This source file is subject to the Open Software License (OSL 3.0) 9 * that is bundled with this package in the file LICENSE.md. 10 * It is also available through the world-wide-web at this URL: 11 * https://opensource.org/licenses/OSL-3.0 12 * If you did not receive a copy of the license and are unable to 13 * obtain it through the world-wide-web, please send an email 14 * to license@prestashop.com so we can send you a copy immediately. 15 * 16 * DISCLAIMER 17 * 18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer 19 * versions in the future. If you wish to customize PrestaShop for your 20 * needs please refer to https://devdocs.prestashop.com/ for more information. 21 * 22 * @author PrestaShop SA and Contributors <contact@prestashop.com> 23 * @copyright Since 2007 PrestaShop SA and Contributors 24 * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) 25 */ 26 27namespace PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataHandler; 28 29use PrestaShop\PrestaShop\Core\CommandBus\CommandBusInterface; 30use PrestaShop\PrestaShop\Core\Domain\Category\Command\AddCategoryCommand; 31use PrestaShop\PrestaShop\Core\Domain\Category\Command\EditCategoryCommand; 32use PrestaShop\PrestaShop\Core\Domain\Category\ValueObject\CategoryId; 33use PrestaShop\PrestaShop\Core\Image\Uploader\ImageUploaderInterface; 34use Symfony\Component\HttpFoundation\File\UploadedFile; 35 36/** 37 * Creates/updates category from data submitted in category form 38 */ 39final class CategoryFormDataHandler implements FormDataHandlerInterface 40{ 41 /** 42 * @var CommandBusInterface 43 */ 44 private $commandBus; 45 46 /** 47 * @var ImageUploaderInterface 48 */ 49 private $categoryCoverUploader; 50 51 /** 52 * @var ImageUploaderInterface 53 */ 54 private $categoryThumbnailUploader; 55 56 /** 57 * @var ImageUploaderInterface 58 */ 59 private $categoryMenuThumbnailUploader; 60 61 /** 62 * @param CommandBusInterface $commandBus 63 * @param ImageUploaderInterface $categoryCoverUploader 64 * @param ImageUploaderInterface $categoryThumbnailUploader 65 * @param ImageUploaderInterface $categoryMenuThumbnailUploader 66 */ 67 public function __construct( 68 CommandBusInterface $commandBus, 69 ImageUploaderInterface $categoryCoverUploader, 70 ImageUploaderInterface $categoryThumbnailUploader, 71 ImageUploaderInterface $categoryMenuThumbnailUploader 72 ) { 73 $this->commandBus = $commandBus; 74 $this->categoryCoverUploader = $categoryCoverUploader; 75 $this->categoryThumbnailUploader = $categoryThumbnailUploader; 76 $this->categoryMenuThumbnailUploader = $categoryMenuThumbnailUploader; 77 } 78 79 /** 80 * {@inheritdoc} 81 */ 82 public function create(array $data) 83 { 84 $command = $this->createAddCategoryCommand($data); 85 86 /** @var CategoryId $categoryId */ 87 $categoryId = $this->commandBus->handle($command); 88 89 $this->uploadImages( 90 $categoryId, 91 $data['cover_image'], 92 $data['thumbnail_image'], 93 $data['menu_thumbnail_images'] 94 ); 95 96 return $categoryId->getValue(); 97 } 98 99 /** 100 * {@inheritdoc} 101 */ 102 public function update($categoryId, array $data) 103 { 104 $command = $this->createEditCategoryCommand($categoryId, $data); 105 106 $this->commandBus->handle($command); 107 108 $categoryId = new CategoryId((int) $categoryId); 109 110 $this->uploadImages( 111 $categoryId, 112 $data['cover_image'], 113 $data['thumbnail_image'], 114 $data['menu_thumbnail_images'] 115 ); 116 } 117 118 /** 119 * Creates add category command from form data 120 * 121 * @param array $data 122 * 123 * @return AddCategoryCommand 124 */ 125 private function createAddCategoryCommand(array $data) 126 { 127 $command = new AddCategoryCommand( 128 $data['name'], 129 $data['link_rewrite'], 130 (bool) $data['active'], 131 (int) $data['id_parent'] 132 ); 133 134 $command->setLocalizedDescriptions($data['description']); 135 $command->setLocalizedMetaTitles($data['meta_title']); 136 $command->setLocalizedMetaDescriptions($data['meta_description']); 137 $command->setLocalizedMetaKeywords($data['meta_keyword']); 138 $command->setAssociatedGroupIds($data['group_association']); 139 140 if (isset($data['shop_association'])) { 141 $command->setAssociatedShopIds($data['shop_association']); 142 } 143 144 return $command; 145 } 146 147 /** 148 * Creates edit category command from 149 * 150 * @param int $categoryId 151 * @param array $data 152 * 153 * @return EditCategoryCommand 154 */ 155 private function createEditCategoryCommand($categoryId, array $data) 156 { 157 $command = new EditCategoryCommand($categoryId); 158 $command->setIsActive($data['active']); 159 $command->setLocalizedLinkRewrites($data['link_rewrite']); 160 $command->setLocalizedNames($data['name']); 161 $command->setParentCategoryId($data['id_parent']); 162 $command->setLocalizedDescriptions($data['description']); 163 $command->setLocalizedMetaTitles($data['meta_title']); 164 $command->setLocalizedMetaDescriptions($data['meta_description']); 165 $command->setLocalizedMetaKeywords($data['meta_keyword']); 166 $command->setAssociatedGroupIds($data['group_association']); 167 168 if (isset($data['shop_association'])) { 169 $command->setAssociatedShopIds($data['shop_association']); 170 } 171 172 return $command; 173 } 174 175 /** 176 * @param CategoryId $categoryId 177 * @param UploadedFile $coverImage 178 * @param UploadedFile $thumbnailImage 179 * @param UploadedFile[] $menuThumbnailImages 180 */ 181 private function uploadImages( 182 CategoryId $categoryId, 183 UploadedFile $coverImage = null, 184 UploadedFile $thumbnailImage = null, 185 array $menuThumbnailImages = [] 186 ) { 187 if (null !== $coverImage) { 188 $this->categoryCoverUploader->upload($categoryId->getValue(), $coverImage); 189 } 190 191 if (null !== $thumbnailImage) { 192 $this->categoryThumbnailUploader->upload($categoryId->getValue(), $thumbnailImage); 193 } 194 195 if (!empty($menuThumbnailImages)) { 196 foreach ($menuThumbnailImages as $menuThumbnail) { 197 $this->categoryMenuThumbnailUploader->upload($categoryId->getValue(), $menuThumbnail); 198 } 199 } 200 } 201} 202