|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the CleverAge/ArchiveProcessBundle package. |
| 7 | + * |
| 8 | + * Copyright (c) Clever-Age |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CleverAge\ArchiveProcessBundle\Task; |
| 15 | + |
| 16 | +use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; |
| 17 | +use CleverAge\ProcessBundle\Model\ProcessState; |
| 18 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 19 | + |
| 20 | +/** |
| 21 | + * Zip files into a given filename. |
| 22 | + */ |
| 23 | +class ZipTask extends AbstractConfigurableTask |
| 24 | +{ |
| 25 | + public function execute(ProcessState $state): void |
| 26 | + { |
| 27 | + if (null === $state->getInput()) { |
| 28 | + $state->setInput([]); |
| 29 | + } |
| 30 | + /** |
| 31 | + * @var array{filename: string, files: array<string>|string, files_base_path: string} $options |
| 32 | + */ |
| 33 | + $options = $this->getOptions($state); |
| 34 | + |
| 35 | + $zip = new \ZipArchive(); |
| 36 | + $ret = $zip->open($options['filename'], \ZipArchive::CREATE | \ZipArchive::OVERWRITE); |
| 37 | + if (true !== $ret) { |
| 38 | + throw new \RuntimeException("Fail to open file {$options['filename']} with code {$ret}"); |
| 39 | + } |
| 40 | + |
| 41 | + $files = $options['files']; |
| 42 | + if (\is_string($files)) { |
| 43 | + $files = [$files]; |
| 44 | + } |
| 45 | + foreach ($files as $file) { |
| 46 | + $currentFilename = ltrim(str_replace($options['files_base_path'], '', $file), \DIRECTORY_SEPARATOR); |
| 47 | + $currentFilepath = $options['files_base_path'].\DIRECTORY_SEPARATOR.$currentFilename; |
| 48 | + if (!file_exists($currentFilepath)) { |
| 49 | + throw new \UnexpectedValueException("File does not exists: '{$currentFilepath}'"); |
| 50 | + } |
| 51 | + if (!is_readable($currentFilepath)) { |
| 52 | + throw new \UnexpectedValueException("File is not readable: '{$currentFilepath}'"); |
| 53 | + } |
| 54 | + if (false === $zip->addFile($currentFilepath, $currentFilename)) { |
| 55 | + throw new \RuntimeException("Unable to add file {$currentFilepath} to zip"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + $zip->close(); |
| 60 | + |
| 61 | + $state->setOutput($options['filename']); |
| 62 | + } |
| 63 | + |
| 64 | + protected function configureOptions(OptionsResolver $resolver): void |
| 65 | + { |
| 66 | + $resolver->setRequired(['filename', 'files', 'files_base_path']); |
| 67 | + $resolver->setAllowedTypes('filename', ['string']); |
| 68 | + $resolver->setAllowedTypes('files', ['string', 'array']); |
| 69 | + $resolver->setAllowedTypes('files_base_path', ['string']); |
| 70 | + $resolver->setDefaults([ |
| 71 | + 'files_base_path' => '', |
| 72 | + ]); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return array{filename: string, files: array<string>|string, files_base_path: string}|null |
| 77 | + */ |
| 78 | + protected function getOptions(ProcessState $state): ?array |
| 79 | + { |
| 80 | + if (null === $this->options && \is_array($state->getInput())) { |
| 81 | + $resolver = new OptionsResolver(); |
| 82 | + $this->configureOptions($resolver); |
| 83 | + $this->options = $resolver->resolve(array_merge($state->getContextualizedOptions() ?? [], $state->getInput())); |
| 84 | + } |
| 85 | + |
| 86 | + // @phpstan-ignore return.type |
| 87 | + return $this->options; |
| 88 | + } |
| 89 | +} |
0 commit comments