Repository navigation
Expand file tree
/
Copy pathInputFolderBrowserTask.php
More file actions
96 lines (82 loc) · 2.9 KB
/
Copy pathInputFolderBrowserTask.php
File metadata and controls
96 lines (82 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\ProcessBundle\Task\File;
use CleverAge\ProcessBundle\Exception\LogicException;
use CleverAge\ProcessBundle\Exception\UnexpectedValueException;
use CleverAge\ProcessBundle\Model\FlushableTaskInterface;
use CleverAge\ProcessBundle\Model\ProcessState;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Browse a folder with the path as an input and iterate each file for output.
*
* @final since 5.4
*/
class InputFolderBrowserTask extends FolderBrowserTask implements FlushableTaskInterface
{
protected ?string $folderPath = null;
public function flush(ProcessState $state): void
{
$this->folderPath = null;
$state->setSkipped(true);
}
#[\Override]
public function next(ProcessState $state): bool
{
$hasNext = parent::next($state);
if (!$hasNext) {
// Release the folder path to allow the following input to browse another folder
$this->folderPath = null;
}
return $hasNext;
}
#[\Override]
public function initialize(ProcessState $state): void
{
parent::getOptions($state);
}
#[\Override]
protected function configureOptions(OptionsResolver $resolver): void
{
parent::configureOptions($resolver);
$resolver->remove(['folder_path']);
$resolver->setDefaults([
'base_folder_path' => '',
]);
$resolver->setAllowedTypes('base_folder_path', ['string']);
}
/**
* @return array<string, mixed>
*/
#[\Override]
protected function getOptions(ProcessState $state): array
{
$options = parent::getOptions($state);
if ($state->getInput()) {
$folderPath = $options['base_folder_path'].$state->getInput();
if ($this->folderPath && $folderPath !== $this->folderPath) {
throw new LogicException("Folder path '{$folderPath}' already initialized with a different value {$this->folderPath}");
}
$this->folderPath = $folderPath;
}
if (null === $this->folderPath) {
throw new UnexpectedValueException('No folder path given as input');
}
if (!is_dir($this->folderPath)) {
throw new InvalidConfigurationException("Folder path does not exists or is not a folder: '{$this->folderPath}'");
}
if (!is_readable($this->folderPath)) {
throw new InvalidConfigurationException("Folder path is not readable: '{$this->folderPath}'");
}
$options['folder_path'] = $this->folderPath;
return $options;
}
}