1
|
|
|
<?php |
2
|
|
|
namespace Local\Event\Listener; |
3
|
|
|
|
4
|
|
|
use Joomla\Registry\Registry; |
5
|
|
|
use League\Flysystem\Adapter\Local; |
6
|
|
|
use Tartana\Domain\Command\ChangeDownloadState; |
7
|
|
|
use Tartana\Domain\Command\SaveDownloads; |
8
|
|
|
use Tartana\Entity\Download; |
9
|
|
|
use Tartana\Event\CommandEvent; |
10
|
|
|
use Tartana\Mixins\CommandBusAwareTrait; |
11
|
|
|
use Tartana\Mixins\LoggerAwareTrait; |
12
|
|
|
use Tartana\Util; |
13
|
|
|
|
14
|
|
|
class ChangeDownloadStateListener |
15
|
|
|
{ |
16
|
|
|
use LoggerAwareTrait; |
17
|
|
|
use CommandBusAwareTrait; |
18
|
|
|
|
19
|
|
|
private $configuration = null; |
20
|
|
|
|
21
|
17 |
|
public function __construct(Registry $configuration) |
22
|
|
|
{ |
23
|
17 |
|
$this->configuration = $configuration; |
24
|
17 |
|
} |
25
|
|
|
|
26
|
17 |
|
public function onChangeDownloadStateAfter(CommandEvent $event) |
27
|
|
|
{ |
28
|
17 |
|
if (!$event->getCommand() instanceof ChangeDownloadState) { |
29
|
14 |
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
7 |
|
if ($event->getCommand()->getToState() != Download::STATE_DOWNLOADING_NOT_STARTED) { |
33
|
3 |
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
4 |
|
$destination = rtrim(Util::realPath($this->configuration->get('downloads')), '/'); |
37
|
|
|
// Something is wrong |
38
|
4 |
|
if (empty($destination)) { |
39
|
1 |
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
3 |
|
$this->log('Checking if all downloads belong to the destination ' . $destination); |
43
|
|
|
|
44
|
3 |
|
$toSave = []; |
45
|
3 |
|
foreach ($event->getCommand()->getDownloads() as $download) { |
46
|
3 |
|
$base = rtrim(dirname($download->getDestination()), '/'); |
47
|
3 |
|
if (strpos($destination, $base) !== false) { |
48
|
1 |
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Destination is different than the download directory |
52
|
3 |
|
$download->setDestination(str_replace($base, $destination, $download->getDestination())); |
53
|
3 |
|
new Local($download->getDestination()); |
54
|
|
|
|
55
|
3 |
|
$toSave[] = $download; |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
if (!empty($toSave)) { |
59
|
3 |
|
$this->handleCommand(new SaveDownloads($toSave)); |
60
|
|
|
} |
61
|
3 |
|
} |
62
|
|
|
} |
63
|
|
|
|