12 use Magento\Customer\Model\FileUploaderFactory;
15 use Psr\Log\LoggerInterface;
29 private $fileUploaderFactory;
34 private $customerMetadataService;
49 FileUploaderFactory $fileUploaderFactory,
51 LoggerInterface $logger
53 $this->fileUploaderFactory = $fileUploaderFactory;
54 $this->customerMetadataService = $customerMetadataService;
56 parent::__construct($context);
66 throw new \Exception(
'$_FILES array is empty.');
70 $attributeMetadata = $this->customerMetadataService->getAttributeMetadata(
$attributeCode);
73 $fileUploader = $this->fileUploaderFactory->create([
74 'attributeMetadata' => $attributeMetadata,
76 'scope' =>
'customer',
79 $errors = $fileUploader->validate();
81 $errorMessage = implode(
'</br>',
$errors);
82 throw new LocalizedException(
__($errorMessage));
85 $result = $fileUploader->upload();
86 }
catch (LocalizedException $e) {
88 'error' => $e->getMessage(),
89 'errorcode' => $e->getCode(),
91 }
catch (\Exception $e) {
92 $this->logger->critical($e);
94 'error' =>
__(
'Something went wrong while saving file.'),
95 'errorcode' => $e->getCode(),
__construct(Context $context, FileUploaderFactory $fileUploaderFactory, CustomerMetadataInterface $customerMetadataService, LoggerInterface $logger)