|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Customer\Controller\Address\File; |
| 9 | + |
| 10 | +use Magento\Framework\App\Action\Action; |
| 11 | +use Magento\Framework\App\Action\Context; |
| 12 | +use Magento\Framework\App\Action\HttpPostActionInterface; |
| 13 | +use Magento\Framework\Api\CustomAttributesDataInterface; |
| 14 | +use Magento\Customer\Api\AddressMetadataInterface; |
| 15 | +use Magento\Customer\Model\FileUploader; |
| 16 | +use Magento\Customer\Model\FileUploaderFactory; |
| 17 | +use Magento\Framework\Controller\ResultFactory; |
| 18 | +use Magento\Framework\Exception\LocalizedException; |
| 19 | +use Psr\Log\LoggerInterface; |
| 20 | +use Magento\Customer\Model\FileProcessorFactory; |
| 21 | + |
| 22 | +/** |
| 23 | + * Class for upload files for customer custom address attributes |
| 24 | + */ |
| 25 | +class Upload extends Action implements HttpPostActionInterface |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var FileUploaderFactory |
| 29 | + */ |
| 30 | + private $fileUploaderFactory; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var AddressMetadataInterface |
| 34 | + */ |
| 35 | + private $addressMetadataService; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var LoggerInterface |
| 39 | + */ |
| 40 | + private $logger; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var FileProcessorFactory |
| 44 | + */ |
| 45 | + private $fileProcessorFactory; |
| 46 | + |
| 47 | + /** |
| 48 | + * @param Context $context |
| 49 | + * @param FileUploaderFactory $fileUploaderFactory |
| 50 | + * @param AddressMetadataInterface $addressMetadataService |
| 51 | + * @param LoggerInterface $logger |
| 52 | + * @param FileProcessorFactory $fileProcessorFactory |
| 53 | + */ |
| 54 | + public function __construct( |
| 55 | + Context $context, |
| 56 | + FileUploaderFactory $fileUploaderFactory, |
| 57 | + AddressMetadataInterface $addressMetadataService, |
| 58 | + LoggerInterface $logger, |
| 59 | + FileProcessorFactory $fileProcessorFactory |
| 60 | + ) { |
| 61 | + $this->fileUploaderFactory = $fileUploaderFactory; |
| 62 | + $this->addressMetadataService = $addressMetadataService; |
| 63 | + $this->logger = $logger; |
| 64 | + $this->fileProcessorFactory = $fileProcessorFactory; |
| 65 | + parent::__construct($context); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @inheritDoc |
| 70 | + */ |
| 71 | + public function execute() |
| 72 | + { |
| 73 | + try { |
| 74 | + $requestedFiles = $this->getRequest()->getFiles('custom_attributes'); |
| 75 | + if (empty($requestedFiles)) { |
| 76 | + $result = $this->processError(__('No files for upload.')); |
| 77 | + } else { |
| 78 | + $attributeCode = key($requestedFiles); |
| 79 | + $attributeMetadata = $this->addressMetadataService->getAttributeMetadata($attributeCode); |
| 80 | + |
| 81 | + /** @var FileUploader $fileUploader */ |
| 82 | + $fileUploader = $this->fileUploaderFactory->create([ |
| 83 | + 'attributeMetadata' => $attributeMetadata, |
| 84 | + 'entityTypeCode' => AddressMetadataInterface::ENTITY_TYPE_ADDRESS, |
| 85 | + 'scope' => CustomAttributesDataInterface::CUSTOM_ATTRIBUTES, |
| 86 | + ]); |
| 87 | + |
| 88 | + $errors = $fileUploader->validate(); |
| 89 | + if (true !== $errors) { |
| 90 | + $errorMessage = implode('</br>', $errors); |
| 91 | + $result = $this->processError(($errorMessage)); |
| 92 | + } else { |
| 93 | + $result = $fileUploader->upload(); |
| 94 | + $this->moveTmpFileToSuitableFolder($result); |
| 95 | + } |
| 96 | + } |
| 97 | + } catch (LocalizedException $e) { |
| 98 | + $result = $this->processError($e->getMessage(), $e->getCode()); |
| 99 | + } catch (\Exception $e) { |
| 100 | + $this->logger->critical($e); |
| 101 | + $result = $this->processError($e->getMessage(), $e->getCode()); |
| 102 | + } |
| 103 | + |
| 104 | + /** @var \Magento\Framework\Controller\Result\Json $resultJson */ |
| 105 | + $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); |
| 106 | + $resultJson->setData($result); |
| 107 | + return $resultJson; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Move file from temporary folder to the 'customer_address' media folder |
| 112 | + * |
| 113 | + * @param array $fileInfo |
| 114 | + * @throws LocalizedException |
| 115 | + */ |
| 116 | + private function moveTmpFileToSuitableFolder(&$fileInfo) |
| 117 | + { |
| 118 | + $fileName = $fileInfo['file']; |
| 119 | + $fileProcessor = $this->fileProcessorFactory |
| 120 | + ->create(['entityTypeCode' => AddressMetadataInterface::ENTITY_TYPE_ADDRESS]); |
| 121 | + |
| 122 | + $newFilePath = $fileProcessor->moveTemporaryFile($fileName); |
| 123 | + $fileInfo['file'] = $newFilePath; |
| 124 | + $fileInfo['url'] = $fileProcessor->getViewUrl( |
| 125 | + $newFilePath, |
| 126 | + 'file' |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Prepare result array for errors |
| 132 | + * |
| 133 | + * @param string $message |
| 134 | + * @param int $code |
| 135 | + * @return array |
| 136 | + */ |
| 137 | + private function processError($message, $code = 0) |
| 138 | + { |
| 139 | + $result = [ |
| 140 | + 'error' => $message, |
| 141 | + 'errorcode' => $code, |
| 142 | + ]; |
| 143 | + |
| 144 | + return $result; |
| 145 | + } |
| 146 | +} |
0 commit comments