-
-
Notifications
You must be signed in to change notification settings - Fork 0
Refractor for better maintability #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
9604b3e
f2371a3
fae3b17
329fcc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ jobs: | |
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Composer run action PHPC_CS | ||
| uses: MayMeowHQ/composer-run-action@v3 | ||
| uses: MayMeowHQ/composer-run-action@v8.4 | ||
| with: | ||
| composer_script: 'cs-check' | ||
|
|
||
|
|
@@ -21,7 +21,17 @@ jobs: | |
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Composer run action PHPStan | ||
| uses: MayMeowHQ/composer-run-action@v3 | ||
| uses: MayMeowHQ/composer-run-action@v8.4 | ||
|
||
| with: | ||
| composer_script: 'stan' | ||
| memory_limit: '1024M' | ||
|
|
||
| test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Composer run action PHPUnit | ||
| uses: MayMeowHQ/[email protected] | ||
|
||
| with: | ||
| composer_script: 'test' | ||
| memory_limit: '1024M' | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| use Cake\Http\Exception\HttpException; | ||
| use FileUpload\File\UploadedFileDecorator; | ||
| use FileUpload\Storage\StorageManagerInterface; | ||
| use Psr\Http\Message\UploadedFileInterface; | ||
|
|
||
| /** | ||
| * Upload component | ||
|
|
@@ -29,27 +30,54 @@ class UploadComponent extends Component | |
| * | ||
| * @var string[] | ||
| */ | ||
| protected array $_allowedStorageTypes = [ | ||
| 'local', 's3', | ||
| ]; | ||
| /** | ||
| * @var \FileUpload\Storage\StorageManagerInterface|null | ||
| */ | ||
| private ?StorageManagerInterface $storageManager = null; | ||
|
|
||
| /** | ||
| * Uploading File to storage and returns info of that file | ||
| * | ||
| * @param \Cake\Http\ServerRequest $serverRequest Server Request | ||
| * @throws HttpException | ||
| * @param \Cake\Controller\Controller $controller Controller instance | ||
| * @throws \Cake\Http\Exception\HttpException When upload data is missing or misconfigured | ||
| */ | ||
| public function getFile(Controller $controller): UploadedFileDecorator | ||
| { | ||
| $uploadedFile = $controller->getRequest()->getData($this->getConfig('fieldName')); | ||
|
|
||
| if (!$uploadedFile instanceof UploadedFileInterface) { | ||
| throw new HttpException('Uploaded file data is missing or invalid.'); | ||
| } | ||
|
|
||
| $sm = $this->_getStorageManager(); | ||
|
|
||
| return $sm->put($controller->getRequest()->getData($this->getConfig('fieldName'))); | ||
| return $sm->put($uploadedFile); | ||
| } | ||
|
|
||
| protected function _getStorageManager(): StorageManagerInterface | ||
| { | ||
| if ($this->storageManager instanceof StorageManagerInterface) { | ||
| return $this->storageManager; | ||
| } | ||
|
|
||
| $sm = $this->getConfig('managerClass'); | ||
|
|
||
| return new $sm($this->getConfig()); | ||
| if (!is_string($sm) || $sm === '') { | ||
| throw new HttpException('Storage manager class is not configured.'); | ||
| } | ||
|
|
||
| if (!class_exists($sm)) { | ||
| throw new HttpException(sprintf('Storage manager class "%s" does not exist.', $sm)); | ||
| } | ||
|
|
||
| $storageManager = new $sm($this->getConfig()); | ||
|
|
||
| if (!$storageManager instanceof StorageManagerInterface) { | ||
| throw new HttpException(sprintf('Storage manager must implement %s.', StorageManagerInterface::class)); | ||
| } | ||
|
|
||
| $this->storageManager = $storageManager; | ||
|
|
||
| return $this->storageManager; | ||
| } | ||
|
Comment on lines
52
to
77
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,18 +3,18 @@ | |||||
|
|
||||||
| namespace FileUpload\File; | ||||||
|
|
||||||
| use Laminas\Diactoros\UploadedFile; | ||||||
| use Psr\Http\Message\UploadedFileInterface; | ||||||
|
|
||||||
| class UploadedFileDecorator | ||||||
| { | ||||||
| public function __construct( | ||||||
| protected UploadedFile $originalData, | ||||||
| protected UploadedFileInterface $originalData, | ||||||
| protected string $storageType, | ||||||
| protected array $options = [] | ||||||
| ) { | ||||||
| } | ||||||
|
|
||||||
| public function getOriginalData(): UploadedFile | ||||||
| public function getOriginalData(): UploadedFileInterface | ||||||
| { | ||||||
| return $this->originalData; | ||||||
| } | ||||||
|
|
@@ -24,15 +24,17 @@ public function getStorageType(): string | |||||
| return $this->storageType; | ||||||
| } | ||||||
|
|
||||||
| public function get(string $key): string | ||||||
| public function get(string $key, mixed $default = null): mixed | ||||||
| { | ||||||
| return $this->options[$key] ?? ""; | ||||||
| return $this->options[$key] ?? $default; | ||||||
| } | ||||||
|
|
||||||
| public function getFileName(): string | ||||||
| { | ||||||
| if (isset($this->options['fileName'])) { | ||||||
| return $this->options['fileName']; | ||||||
| $fileName = $this->get('fileName'); | ||||||
|
|
||||||
| if (is_string($fileName) && $fileName !== '') { | ||||||
| return $fileName; | ||||||
| } | ||||||
|
|
||||||
| return $this->originalData->getClientFilename(); | ||||||
|
||||||
| return $this->originalData->getClientFilename(); | |
| return $this->originalData->getClientFilename() ?? ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The version
v8.4for this GitHub Action is unusual. GitHub Actions typically use either major version tags (e.g.,v3,v8) or full semantic versions (e.g.,v3.2.1). Please verify thatv8.4is a valid and existing tag in theMayMeowHQ/composer-run-actionrepository.