|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/** |
| 3 | + * This file is part of the CleverAge/FlysystemProcessBundle package. |
| 4 | + * |
| 5 | + * Copyright (C) 2017-2019 Clever-Age |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace CleverAge\FlysystemProcessBundle\Task; |
| 12 | + |
| 13 | +use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; |
| 14 | +use CleverAge\ProcessBundle\Model\IterableTaskInterface; |
| 15 | +use CleverAge\ProcessBundle\Model\ProcessState; |
| 16 | +use League\Flysystem\FileNotFoundException; |
| 17 | +use League\Flysystem\FilesystemInterface; |
| 18 | +use League\Flysystem\FilesystemNotFoundException; |
| 19 | +use League\Flysystem\MountManager; |
| 20 | +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
| 21 | +use Symfony\Component\OptionsResolver\Exception\ExceptionInterface; |
| 22 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 23 | + |
| 24 | +/** |
| 25 | + * Copy (or move) file from one filesystem to another, using Flysystem |
| 26 | + * Either get files using a file regexp, or take files from input |
| 27 | + * |
| 28 | + * @author Madeline Veyrenc <[email protected]> |
| 29 | + */ |
| 30 | +class FileFetchTask extends AbstractConfigurableTask implements IterableTaskInterface |
| 31 | +{ |
| 32 | + /** @var MountManager */ |
| 33 | + protected $mountManager; |
| 34 | + |
| 35 | + /** @var FilesystemInterface */ |
| 36 | + protected $sourceFS; |
| 37 | + |
| 38 | + /** @var FilesystemInterface */ |
| 39 | + protected $destinationFS; |
| 40 | + |
| 41 | + /** @var array */ |
| 42 | + protected $matchingFiles = []; |
| 43 | + |
| 44 | + /** |
| 45 | + * @param MountManager|null $mountManager |
| 46 | + */ |
| 47 | + public function __construct(MountManager $mountManager = null) |
| 48 | + { |
| 49 | + $this->mountManager = $mountManager; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param ProcessState $state |
| 54 | + * |
| 55 | + * @throws \InvalidArgumentException |
| 56 | + * @throws ExceptionInterface |
| 57 | + * @throws FilesystemNotFoundException |
| 58 | + */ |
| 59 | + public function initialize(ProcessState $state) |
| 60 | + { |
| 61 | + if (!$this->mountManager) { |
| 62 | + throw new ServiceNotFoundException('MountManager service not found, you need to install FlySystemBundle'); |
| 63 | + } |
| 64 | + // Configure options |
| 65 | + parent::initialize($state); |
| 66 | + |
| 67 | + $this->sourceFS = $this->mountManager->getFilesystem($this->getOption($state, 'source_filesystem')); |
| 68 | + $this->destinationFS = $this->mountManager->getFilesystem($this->getOption($state, 'destination_filesystem')); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @param ProcessState $state |
| 73 | + * |
| 74 | + * @throws \InvalidArgumentException |
| 75 | + * @throws ExceptionInterface |
| 76 | + * @throws \UnexpectedValueException |
| 77 | + * @throws FilesystemNotFoundException |
| 78 | + * @throws FileNotFoundException |
| 79 | + */ |
| 80 | + public function execute(ProcessState $state): void |
| 81 | + { |
| 82 | + $this->findMatchingFiles($state); |
| 83 | + |
| 84 | + $file = current($this->matchingFiles); |
| 85 | + if (!$file) { |
| 86 | + $state->setSkipped(true); |
| 87 | + |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + $this->doFileCopy($state, $file, $this->getOption($state, 'remove_source')); |
| 92 | + $state->setOutput($file); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param ProcessState $state |
| 97 | + * |
| 98 | + * @throws \UnexpectedValueException |
| 99 | + * @throws ExceptionInterface |
| 100 | + * @throws \InvalidArgumentException |
| 101 | + * |
| 102 | + * @return bool|mixed |
| 103 | + */ |
| 104 | + public function next(ProcessState $state) |
| 105 | + { |
| 106 | + $this->findMatchingFiles($state); |
| 107 | + |
| 108 | + return next($this->matchingFiles); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param ProcessState $state |
| 113 | + * |
| 114 | + * @throws \UnexpectedValueException |
| 115 | + * @throws \InvalidArgumentException |
| 116 | + * @throws ExceptionInterface |
| 117 | + */ |
| 118 | + protected function findMatchingFiles(ProcessState $state) |
| 119 | + { |
| 120 | + $filePattern = $this->getOption($state, 'file_pattern'); |
| 121 | + if ($filePattern) { |
| 122 | + foreach ($this->sourceFS->listContents('/') as $file) { |
| 123 | + if ('file' === $file['type'] |
| 124 | + && preg_match($filePattern, $file['path']) |
| 125 | + && !\in_array($file['path'], $this->matchingFiles, true)) { |
| 126 | + $this->matchingFiles[] = $file['path']; |
| 127 | + } |
| 128 | + } |
| 129 | + } else { |
| 130 | + $input = $state->getInput(); |
| 131 | + if (!$input) { |
| 132 | + throw new \UnexpectedValueException('No pattern neither input provided for the Task'); |
| 133 | + } |
| 134 | + if (\is_array($input)) { |
| 135 | + foreach ($input as $file) { |
| 136 | + if (!\in_array($file, $this->matchingFiles, true)) { |
| 137 | + $this->matchingFiles[] = $file; |
| 138 | + } |
| 139 | + } |
| 140 | + } elseif (!\in_array($input, $this->matchingFiles, true)) { |
| 141 | + $this->matchingFiles[] = $input; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @param ProcessState $state |
| 148 | + * @param string $filename |
| 149 | + * @param bool $removeSource |
| 150 | + * |
| 151 | + * @throws FileNotFoundException |
| 152 | + * @throws ExceptionInterface |
| 153 | + * @throws FilesystemNotFoundException |
| 154 | + * @throws \InvalidArgumentException |
| 155 | + * |
| 156 | + * @return mixed |
| 157 | + */ |
| 158 | + protected function doFileCopy(ProcessState $state, $filename, $removeSource) |
| 159 | + { |
| 160 | + $prefixFrom = $this->getOption($state, 'source_filesystem'); |
| 161 | + $prefixTo = $this->getOption($state, 'destination_filesystem'); |
| 162 | + |
| 163 | + $buffer = $this->mountManager->getFilesystem($prefixFrom)->readStream($filename); |
| 164 | + |
| 165 | + if (false === $buffer) { |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | + $result = $this->mountManager->getFilesystem($prefixTo)->putStream($filename, $buffer); |
| 170 | + |
| 171 | + if (\is_resource($buffer)) { |
| 172 | + fclose($buffer); |
| 173 | + } |
| 174 | + |
| 175 | + if ($removeSource) { |
| 176 | + $this->mountManager->delete(sprintf('%s://%s', $prefixFrom, $filename)); |
| 177 | + } |
| 178 | + |
| 179 | + return $result ? $filename : null; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * {@inheritdoc} |
| 184 | + */ |
| 185 | + protected function configureOptions(OptionsResolver $resolver) |
| 186 | + { |
| 187 | + $resolver->setRequired(['source_filesystem', 'destination_filesystem']); |
| 188 | + $resolver->setAllowedTypes('source_filesystem', 'string'); |
| 189 | + $resolver->setAllowedTypes('destination_filesystem', 'string'); |
| 190 | + |
| 191 | + $resolver->setDefault('file_pattern', null); |
| 192 | + $resolver->setAllowedTypes('file_pattern', ['string', 'null']); |
| 193 | + |
| 194 | + $resolver->setDefault('remove_source', false); |
| 195 | + $resolver->setAllowedTypes('remove_source', 'boolean'); |
| 196 | + } |
| 197 | +} |
0 commit comments