Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileUploader.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Customer\Model;
7 
11 use Magento\Customer\Model\FileProcessorFactory;
14 
16 {
20  private $customerMetadataService;
21 
25  private $addressMetadataService;
26 
30  private $elementFactory;
31 
35  private $fileProcessorFactory;
36 
40  private $attributeMetadata;
41 
45  private $entityTypeCode;
46 
50  private $scope;
51 
61  public function __construct(
62  CustomerMetadataInterface $customerMetadataService,
63  AddressMetadataInterface $addressMetadataService,
64  ElementFactory $elementFactory,
65  FileProcessorFactory $fileProcessorFactory,
66  AttributeMetadataInterface $attributeMetadata,
67  $entityTypeCode,
68  $scope
69  ) {
70  $this->customerMetadataService = $customerMetadataService;
71  $this->addressMetadataService = $addressMetadataService;
72  $this->elementFactory = $elementFactory;
73  $this->fileProcessorFactory = $fileProcessorFactory;
74  $this->attributeMetadata = $attributeMetadata;
75  $this->entityTypeCode = $entityTypeCode;
76  $this->scope = $scope;
77  }
78 
84  public function validate()
85  {
86  $formElement = $this->elementFactory->create(
87  $this->attributeMetadata,
88  null,
89  $this->entityTypeCode
90  );
91 
92  $errors = $formElement->validateValue($this->getData());
93  return $errors;
94  }
95 
102  public function upload()
103  {
105  $fileProcessor = $this->fileProcessorFactory->create([
106  'entityTypeCode' => $this->entityTypeCode,
107  'allowedExtensions' => $this->getAllowedExtensions(),
108  ]);
109 
110  $result = $fileProcessor->saveTemporaryFile($this->scope . '[' . $this->getAttributeCode() . ']');
111 
112  // Update tmp_name param. Required for attribute validation!
113  $result['tmp_name'] = ltrim($result['file'], '/');
114 
115  $result['url'] = $fileProcessor->getViewUrl(
116  FileProcessor::TMP_DIR . '/' . ltrim($result['name'], '/'),
117  $this->attributeMetadata->getFrontendInput()
118  );
119 
120  return $result;
121  }
122 
128  private function getAttributeCode()
129  {
130  return key($_FILES[$this->scope]['name']);
131  }
132 
138  private function getData()
139  {
140  $data = [];
141 
142  $fileAttributes = $_FILES[$this->scope];
143  foreach ($fileAttributes as $attributeName => $attributeValue) {
144  $data[$attributeName] = $attributeValue[$this->getAttributeCode()];
145  }
146 
147  return $data;
148  }
149 
155  private function getAllowedExtensions()
156  {
157  $allowedExtensions = [];
158 
159  $validationRules = $this->attributeMetadata->getValidationRules();
160  foreach ($validationRules as $validationRule) {
161  if ($validationRule->getName() == 'file_extensions') {
162  $allowedExtensions = explode(',', $validationRule->getValue());
163  array_walk($allowedExtensions, function (&$value) {
164  $value = strtolower(trim($value));
165  });
166  break;
167  }
168  }
169 
170  return $allowedExtensions;
171  }
172 }
$allowedExtensions
Definition: uploader.phtml:12
$value
Definition: gender.phtml:16
__construct(CustomerMetadataInterface $customerMetadataService, AddressMetadataInterface $addressMetadataService, ElementFactory $elementFactory, FileProcessorFactory $fileProcessorFactory, AttributeMetadataInterface $attributeMetadata, $entityTypeCode, $scope)
$errors
Definition: overview.phtml:9