Completed
Push — master ( 94e04a...7e753b )
by C
05:18
created

UpdateExtractStateListener::onProcessingCompleted()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4
Metric Value
dl 0
loc 24
ccs 14
cts 14
cp 1
rs 8.6845
cc 4
eloc 14
nc 6
nop 1
crap 4
1
<?php
2
namespace Tartana\Event\Listener;
3
use Tartana\Domain\Command\SaveDownloads;
4
use Tartana\Domain\DownloadRepository;
5
use Tartana\Entity\Download;
6
use Tartana\Event\ProcessingCompletedEvent;
7
use Tartana\Event\ProcessingProgressEvent;
8
use Tartana\Mixins\LoggerAwareTrait;
9
use SimpleBus\Message\Bus\MessageBus;
10
11
class UpdateExtractStateListener
12
{
13
14
	use LoggerAwareTrait;
15
16
	private $repository = null;
17
18
	private $commandBus = null;
19
20 3
	public function __construct (DownloadRepository $repository, MessageBus $commandBus)
21
	{
22 3
		$this->repository = $repository;
23 3
		$this->commandBus = $commandBus;
24 3
	}
25
26 1
	public function onExtractProgress (ProcessingProgressEvent $event)
27
	{
28 1
		$downloads = $this->repository->findDownloadsByDestination($event->getSource()
29 1
			->getPathPrefix());
30
31 1
		$hasChanged = false;
32 1
		foreach ($downloads as $download)
33
		{
34 1
			if ($download->getFileName() != $event->getFile())
35
			{
36 1
				continue;
37
			}
38 1
			$download->setProgress($event->getProgress());
39 1
			$hasChanged = true;
40
		}
41 1
		if ($hasChanged)
42
		{
43 1
			$this->commandBus->handle(new SaveDownloads($downloads));
44
		}
45 1
	}
46
47 2
	public function onProcessingCompleted (ProcessingCompletedEvent $event)
48
	{
49 2
		$downloads = $this->repository->findDownloadsByDestination($event->getSource()
50 2
			->getPathPrefix());
51 2
		$hasChanged = false;
52 2
		foreach ($downloads as $download)
53
		{
54 2
			if ($event->isSuccess())
55
			{
56 1
				$download->setState(Download::STATE_PROCESSING_COMPLETED);
57
			}
58
			else
59
			{
60 1
				$download->setState(Download::STATE_PROCESSING_ERROR);
61 1
				$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_EXTRACT_FAILED');
62
			}
63 2
			$download->setProgress(100);
64 2
			$hasChanged = true;
65
		}
66 2
		if ($hasChanged)
67
		{
68 2
			$this->commandBus->handle(new SaveDownloads($downloads));
69
		}
70 2
	}
71
}
72