Completed
Push — master ( 1e5574...2c7872 )
by C
06:04
created

Localhost::fetchDownloadInfo()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 10
nc 4
nop 1
1
<?php
2
namespace Tartana\Host;
3
use Joomla\Registry\Registry;
4
use League\Flysystem\Adapter\Local;
5
use League\Flysystem\Filesystem;
6
use League\Flysystem\MountManager;
7
use Tartana\Domain\Command\SaveDownloads;
8
use Tartana\Entity\Download;
9
use Tartana\Mixins\CommandBusAwareTrait;
10
use Tartana\Mixins\LoggerAwareTrait;
11
use Tartana\Util;
12
13
class Localhost implements HostInterface
14
{
15
	use LoggerAwareTrait;
16
	use CommandBusAwareTrait;
17
18
	private $configuration = null;
19
20
	public function __construct (Registry $configuration = null)
21
	{
22
		$this->configuration = $configuration;
23
	}
24
25
	public function fetchDownloadInfo (array $downloads)
26
	{
27
		foreach ($downloads as $download)
28
		{
29
			if ($download->getFileName())
30
			{
31
				continue;
32
			}
33
34
			$path = $this->getFileName($download->getLink());
35
			if (empty($path))
36
			{
37
				$this->updateDownload($download, 'TARTANA_DOWNLOAD_MESSAGE_INVALID_PATH', Download::STATE_DOWNLOADING_ERROR);
38
				continue;
39
			}
40
41
			$fs = new Local(dirname($path));
42
			$download->setFileName($fs->removePathPrefix($path));
43
		}
44
	}
45
46
	public function download (array $downloads)
47
	{
48
		foreach ($downloads as $download)
49
		{
50
			$destination = Util::realPath($download->getDestination());
51
			if (empty($destination))
52
			{
53
				$this->updateDownload($download, 'TARTANA_DOWNLOAD_MESSAGE_INVALID_DESTINATION');
54
				continue;
55
			}
56
57
			$path = $this->getFileName($download->getLink());
58
			if (empty($path))
59
			{
60
				$this->updateDownload($download, 'TARTANA_DOWNLOAD_MESSAGE_INVALID_PATH');
61
				continue;
62
			}
63
64
			$fs = new Local(dirname($path));
65
			$fileName = $fs->removePathPrefix($path);
66
67
			$src = new Filesystem($fs);
68
			$dest = new Filesystem(new Local($destination));
69
70
			$manager = new MountManager([
71
					'src' => $src,
72
					'dest' => $dest
73
			]);
74
75
			if (! @$manager->copy('src://' . $fileName, 'dest://' . ($download->getFileName() ? $download->getFileName() : $fileName)))
76
			{
77
				$this->updateDownload($download, 'TARTANA_DOWNLOAD_MESSAGE_COPY_FAILED');
78
			}
79
			else
80
			{
81
				$download->setFinishedAt(new \DateTime());
82
				$this->updateDownload($download, null, Download::STATE_DOWNLOADING_COMPLETED);
83
			}
84
		}
85
86
		return [];
87
	}
88
89
	private function getFileName ($link)
90
	{
91
		$uri = parse_url($link);
92
		if (! isset($uri['path']))
93
		{
94
			return null;
95
		}
96
97
		$path = Util::realPath($uri['path']);
98
		if (empty($path))
99
		{
100
			// Perhaps relative
101
			$path = Util::realPath(ltrim($uri['path'], '/'));
102
			if (empty($path))
103
			{
104
				return null;
105
			}
106
		}
107
108
		return $path;
109
	}
110
111
	private function updateDownload ($download, $message, $state = Download::STATE_DOWNLOADING_ERROR)
112
	{
113
		$download->setMessage($message);
114
		$download->setState($state);
115
		$this->handleCommand(new SaveDownloads([
116
				$download
117
		]));
118
	}
119
}