1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staticus\Resources\Commands; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\FilesystemInterface; |
6
|
|
|
use Staticus\Resources\Exceptions\CommandErrorException; |
7
|
|
|
use Staticus\Resources\ResourceDOInterface; |
8
|
|
|
|
9
|
|
|
class CopyResourceCommand implements ResourceCommandInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var ResourceDOInterface |
13
|
|
|
*/ |
14
|
|
|
protected $originResourceDO; |
15
|
|
|
/** |
16
|
|
|
* @var ResourceDOInterface |
17
|
|
|
*/ |
18
|
|
|
protected $newResourceDO; |
19
|
|
|
/** |
20
|
|
|
* @var FilesystemInterface |
21
|
|
|
*/ |
22
|
|
|
protected $filesystem; |
23
|
|
|
|
24
|
14 |
|
public function __construct(ResourceDOInterface $originResourceDO, ResourceDOInterface $newResourceDO, FilesystemInterface $filesystem) |
25
|
|
|
{ |
26
|
14 |
|
$this->originResourceDO = $originResourceDO; |
27
|
14 |
|
$this->newResourceDO = $newResourceDO; |
28
|
14 |
|
$this->filesystem = $filesystem; |
29
|
14 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param bool $replace Replace exist file or just do nothing |
33
|
|
|
* @return ResourceDOInterface |
34
|
|
|
* @throws CommandErrorException |
35
|
|
|
*/ |
36
|
14 |
|
public function __invoke($replace = false) |
37
|
|
|
{ |
38
|
14 |
|
if (!$this->originResourceDO->getName() || !$this->originResourceDO->getType()) { |
39
|
1 |
|
throw new CommandErrorException('Source resource cannot be empty'); |
40
|
|
|
} |
41
|
13 |
|
if (!$this->newResourceDO->getName() || !$this->newResourceDO->getType()) { |
42
|
1 |
|
throw new CommandErrorException('Destination resource cannot be empty'); |
43
|
|
|
} |
44
|
12 |
|
$originPath = $this->originResourceDO->getFilePath(); |
45
|
12 |
|
$newPath = $this->newResourceDO->getFilePath(); |
46
|
12 |
|
if ($originPath === $newPath) { |
47
|
1 |
|
throw new CommandErrorException('Source and destination paths is equal'); |
48
|
|
|
} |
49
|
11 |
|
if (!$this->filesystem->has($originPath)) { |
50
|
1 |
|
throw new CommandErrorException('Origin file is not exists: ' . $originPath); |
51
|
|
|
} |
52
|
10 |
|
$exists = $this->filesystem->has($newPath); |
53
|
10 |
|
if (!$exists || $replace) { |
54
|
9 |
|
$this->copyFile($originPath, $newPath, $exists && $replace); |
55
|
|
|
|
56
|
9 |
|
return $this->newResourceDO; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return $this->originResourceDO; |
60
|
|
|
} |
61
|
|
|
|
62
|
9 |
|
protected function copyFile($fromFullPath, $toFullPath, $replace = false) |
63
|
|
|
{ |
64
|
9 |
|
$this->createDirectory(dirname($toFullPath)); |
65
|
9 |
|
if ($replace) { |
66
|
1 |
|
$this->filesystem->delete($toFullPath); |
67
|
1 |
|
} |
68
|
9 |
|
if (!$this->filesystem->copy($fromFullPath, $toFullPath)) { |
69
|
|
|
throw new CommandErrorException('File cannot be copied to the path ' . $toFullPath); |
70
|
|
|
} |
71
|
9 |
|
} |
72
|
|
|
|
73
|
9 |
|
protected function createDirectory($directory) |
74
|
|
|
{ |
75
|
9 |
|
if (!$this->filesystem->createDir($directory)) { |
76
|
|
|
throw new CommandErrorException('Can\'t create a directory: ' . $directory); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |