forked from thephpleague/flysystem-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractTransferCommand.php
More file actions
165 lines (130 loc) · 5.79 KB
/
AbstractTransferCommand.php
File metadata and controls
165 lines (130 loc) · 5.79 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
* This file is part of the flysystem-bundle project.
*
* (c) Titouan Galopin <galopintitouan@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\FlysystemBundle\Command;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ServiceLocator;
abstract class AbstractTransferCommand extends Command
{
public function __construct(private readonly ServiceLocator $storages)
{
parent::__construct();
}
protected function configure(): void
{
$this
->addArgument('storage', InputArgument::REQUIRED, 'The configured Flysystem storage name.')
->addArgument('source', InputArgument::REQUIRED, 'The source path to transfer.')
->addArgument('destination', InputArgument::OPTIONAL, 'The destination path. Defaults to the source basename.');
}
protected function interact(InputInterface $input, OutputInterface $output): void
{
$io = new SymfonyStyle($input, $output);
if (null === $input->getArgument('storage')) {
$input->setArgument('storage', $io->askQuestion($this->createStorageQuestion()));
}
if (null === $input->getArgument('source')) {
$input->setArgument('source', $io->askQuestion($this->createRequiredQuestion(
'What is the source path to transfer?',
'The source path cannot be empty.'
)));
}
if (null === $input->getArgument('destination')) {
$input->setArgument('destination', $io->askQuestion($this->createDestinationQuestion((string) $input->getArgument('source'))));
}
}
final protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$storageName = (string) $input->getArgument('storage');
$source = (string) $input->getArgument('source');
$destination = $input->getArgument('destination');
$destination = null === $destination ? basename($source) : (string) $destination;
$destination = $this->normalizeDestination($source, $destination);
try {
$storage = $this->getStorage($storageName);
$this->transfer($storage, $source, $destination);
} catch (InvalidArgumentException|\InvalidArgumentException $exception) {
$io->error($exception->getMessage());
return self::INVALID;
} catch (FilesystemException|\RuntimeException $exception) {
$io->error($exception->getMessage());
return self::FAILURE;
}
$io->success($this->createSuccessMessage($storageName, $source, $destination));
return self::SUCCESS;
}
private function createStorageQuestion(): Question
{
$storageNames = array_keys($this->storages->getProvidedServices());
sort($storageNames);
$question = new Question('Which configured Flysystem storage should be used?', 1 === count($storageNames) ? $storageNames[0] : null);
$question->setAutocompleterValues($storageNames);
$question->setValidator(function (?string $answer): string {
$answer = trim((string) $answer);
if ('' === $answer) {
throw new \RuntimeException('The storage name cannot be empty.');
}
if (!$this->storages->has($answer)) {
throw new \RuntimeException(sprintf('The storage "%s" does not exist.', $answer));
}
return $answer;
});
return $question;
}
private function createDestinationQuestion(string $source): Question
{
$question = new Question('What is the destination path?', basename($source));
$question->setValidator(function (?string $answer): string {
$answer = trim((string) $answer);
if ('' === $answer) {
throw new \RuntimeException('The destination path cannot be empty.');
}
return $answer;
});
return $question;
}
private function createRequiredQuestion(string $label, string $errorMessage): Question
{
$question = new Question($label);
$question->setValidator(function (?string $answer) use ($errorMessage): string {
$answer = trim((string) $answer);
if ('' === $answer) {
throw new \RuntimeException($errorMessage);
}
return $answer;
});
return $question;
}
private function getStorage(string $storageName): FilesystemOperator
{
if (!$this->storages->has($storageName)) {
throw new InvalidArgumentException(sprintf('The storage "%s" does not exist.', $storageName));
}
$storage = $this->storages->get($storageName);
if (!$storage instanceof FilesystemOperator) {
throw new \RuntimeException(sprintf('The storage "%s" is not a Flysystem operator.', $storageName));
}
return $storage;
}
protected function normalizeDestination(string $source, string $destination): string
{
return $destination;
}
abstract protected function transfer(FilesystemOperator $storage, string $source, string $destination): void;
abstract protected function createSuccessMessage(string $storageName, string $source, string $destination): string;
}