1
|
|
|
<?php |
2
|
|
|
namespace Staticus\Resources\Commands; |
3
|
|
|
|
4
|
|
|
use League\Flysystem\FilesystemInterface; |
5
|
|
|
use Staticus\Resources\Exceptions\CommandErrorException; |
6
|
|
|
use Staticus\Resources\ResourceDOInterface; |
7
|
|
|
|
8
|
|
|
class DestroyEqualResourceCommand implements ResourceCommandInterface |
9
|
|
|
{ |
10
|
|
|
const HASH_ALGORITHM = 'md5'; |
11
|
|
|
/** |
12
|
|
|
* @var ResourceDOInterface |
13
|
|
|
*/ |
14
|
|
|
protected $originResourceDO; |
15
|
|
|
/** |
16
|
|
|
* @var ResourceDOInterface |
17
|
|
|
*/ |
18
|
|
|
protected $suspectResourceDO; |
19
|
|
|
/** |
20
|
|
|
* @var FilesystemInterface |
21
|
|
|
*/ |
22
|
|
|
protected $filesystem; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param ResourceDOInterface $originResourceDO This is a model resource for comparing |
26
|
|
|
* @param ResourceDOInterface $suspectResourceDO This resource will be deleted, if it's equal to $originResourceDO |
27
|
|
|
* @param FilesystemInterface $filesystem |
28
|
|
|
*/ |
29
|
7 |
|
public function __construct(ResourceDOInterface $originResourceDO, ResourceDOInterface $suspectResourceDO, FilesystemInterface $filesystem) |
30
|
|
|
{ |
31
|
7 |
|
$this->originResourceDO = $originResourceDO; |
32
|
7 |
|
$this->suspectResourceDO = $suspectResourceDO; |
33
|
7 |
|
$this->filesystem = $filesystem; |
34
|
7 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return ResourceDOInterface SuspectResource if it have been deleted or OriginResource if the Suspect is not equal |
38
|
|
|
*/ |
39
|
7 |
|
public function __invoke() |
40
|
|
|
{ |
41
|
7 |
|
$originName = $this->originResourceDO->getName(); |
42
|
7 |
|
$suspectName = $this->suspectResourceDO->getName(); |
43
|
7 |
|
$originType = $this->originResourceDO->getType(); |
44
|
7 |
|
$suspectType = $this->suspectResourceDO->getType(); |
45
|
7 |
|
$originFilePath = $this->originResourceDO->getFilePath(); |
46
|
7 |
|
$suspectFilePath = $this->suspectResourceDO->getFilePath(); |
47
|
|
|
|
48
|
7 |
|
if (!$originName || !$originType) { |
49
|
1 |
|
throw new CommandErrorException('Cannot destroy equal resource: the origin resource is empty'); |
50
|
|
|
} |
51
|
6 |
|
if (!$suspectName || !$suspectType) { |
52
|
1 |
|
throw new CommandErrorException('Cannot destroy equal resource: the suspect resource is empty'); |
53
|
|
|
} |
54
|
5 |
|
if ($originFilePath === $suspectFilePath) { |
55
|
1 |
|
throw new CommandErrorException('Cannot destroy equal resource: Origin and Suspect have same paths'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Unfortunately, this condition can not always work fine. |
59
|
|
|
// Because some Middlewares can compress, resize etc. the resource that saved before |
60
|
|
|
// and the second uploaded copy will never be equal |
61
|
|
|
if ($originType === $suspectType |
62
|
4 |
|
&& $this->filesystem->has($originFilePath) === $this->filesystem->has($suspectFilePath) |
63
|
4 |
|
&& $this->filesystem->getSize($originFilePath) === $this->filesystem->getSize($suspectFilePath) |
64
|
4 |
|
&& $this->getFileHash($originFilePath) === $this->getFileHash($suspectFilePath) |
65
|
4 |
|
) { |
66
|
2 |
|
$command = new DestroyResourceCommand($this->suspectResourceDO, $this->filesystem); |
67
|
|
|
|
68
|
2 |
|
return $command(true); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
return $this->originResourceDO; |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
public function getFileHash($path) |
75
|
|
|
{ |
76
|
2 |
|
$stream = $this->filesystem->readStream($path); |
77
|
2 |
|
if ($stream !== false) { |
78
|
2 |
|
$context = hash_init(self::HASH_ALGORITHM); |
79
|
2 |
|
hash_update_stream($context, $stream); |
80
|
|
|
|
81
|
2 |
|
return hash_final($context); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
} |