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