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( |
25
|
|
|
ResourceDOInterface $originResourceDO |
26
|
|
|
, ResourceDOInterface $newResourceDO |
27
|
|
|
, FilesystemInterface $filesystem |
28
|
|
|
) |
29
|
|
|
{ |
30
|
14 |
|
$this->originResourceDO = $originResourceDO; |
31
|
14 |
|
$this->newResourceDO = $newResourceDO; |
32
|
14 |
|
$this->filesystem = $filesystem; |
33
|
14 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param bool $replace Replace exist file or just do nothing |
37
|
|
|
* @return ResourceDOInterface |
38
|
|
|
* @throws CommandErrorException |
39
|
|
|
*/ |
40
|
14 |
|
public function __invoke($replace = false) |
41
|
|
|
{ |
42
|
14 |
|
if (!$this->originResourceDO->getName() || !$this->originResourceDO->getType()) { |
43
|
1 |
|
throw new CommandErrorException('Source resource cannot be empty'); |
44
|
|
|
} |
45
|
13 |
|
if (!$this->newResourceDO->getName() || !$this->newResourceDO->getType()) { |
46
|
1 |
|
throw new CommandErrorException('Destination resource cannot be empty'); |
47
|
|
|
} |
48
|
12 |
|
$originPath = $this->originResourceDO->getFilePath(); |
49
|
12 |
|
$newPath = $this->newResourceDO->getFilePath(); |
50
|
12 |
|
if ($originPath === $newPath) { |
51
|
1 |
|
throw new CommandErrorException('Source and destination paths is equal'); |
52
|
|
|
} |
53
|
11 |
|
if (!$this->filesystem->has($originPath)) { |
54
|
1 |
|
throw new CommandErrorException('Origin file is not exists: ' . $originPath); |
55
|
|
|
} |
56
|
10 |
|
$exists = $this->filesystem->has($newPath); |
57
|
10 |
|
if (!$exists || $replace) { |
58
|
9 |
|
$this->copyFile($originPath, $newPath, $exists && $replace); |
59
|
|
|
|
60
|
9 |
|
return $this->newResourceDO; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
return $this->originResourceDO; |
64
|
|
|
} |
65
|
|
|
|
66
|
9 |
|
protected function copyFile($fromFullPath, $toFullPath, $replace = false) |
67
|
|
|
{ |
68
|
9 |
|
$this->createDirectory(dirname($toFullPath)); |
69
|
9 |
|
if ($replace) { |
70
|
1 |
|
$this->filesystem->delete($toFullPath); |
71
|
1 |
|
} |
72
|
9 |
|
if (!$this->filesystem->copy($fromFullPath, $toFullPath)) { |
73
|
|
|
throw new CommandErrorException('File cannot be copied to the path ' . $toFullPath); |
74
|
|
|
} |
75
|
9 |
|
} |
76
|
|
|
|
77
|
9 |
|
protected function createDirectory($directory) |
78
|
|
|
{ |
79
|
9 |
|
if (!$this->filesystem->createDir($directory)) { |
80
|
|
|
throw new CommandErrorException('Can\'t create a directory: ' . $directory); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |