Completed
Push — master ( 8528a2...610c9c )
by C
05:11
created

Localhost::fetchDownloadInfo()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 26
ccs 12
cts 12
cp 1
rs 8.439
cc 5
eloc 12
nc 5
nop 1
crap 5
1
<?php
2
namespace Tartana\Host;
3
use GuzzleHttp\Promise\Promise;
4
use Joomla\Registry\Registry;
5
use League\Flysystem\Adapter\Local;
6
use League\Flysystem\Filesystem;
7
use League\Flysystem\MountManager;
8
use Tartana\Domain\Command\SaveDownloads;
9
use Tartana\Entity\Download;
10
use Tartana\Mixins\CommandBusAwareTrait;
11
use Tartana\Mixins\LoggerAwareTrait;
12
use Tartana\Util;
13
14
class Localhost implements HostInterface
15
{
16
	use LoggerAwareTrait;
17
	use CommandBusAwareTrait;
18
19
	private $configuration = null;
20
21
	private $manager = null;
22
23 26
	public function __construct (Registry $configuration = null, MountManager $manager = null)
24
	{
25 26
		$this->configuration = $configuration;
26
27 26
		if (empty($manager))
28
		{
29 15
			$manager = new MountManager();
30
		}
31 26
		$this->manager = $manager;
32 26
	}
33
34 3
	public function fetchDownloadInfo (array $downloads)
35
	{
36 3
		foreach ($downloads as $download)
37
		{
38 3
			if ($download->getFileName())
39
			{
40 1
				continue;
41
			}
42
43 2
			$fs = $this->getSourceAdapter($download);
44 2
			if (! empty($fs))
45
			{
46 1
				$download->setFileName(basename($download->getLink()));
47
48 1
				if ($fs instanceof Local)
49
				{
50 1
					$download->setHash(md5_file($download->getLink()));
51
				}
52
			}
53
			else
54
			{
55 1
				$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_PATH');
56 2
				$download->setState(Download::STATE_DOWNLOADING_ERROR);
57
			}
58
		}
59 3
	}
60
61 22
	public function download (array $downloads)
62
	{
63 22
		$promises = [];
64 22
		foreach ($downloads as $download)
65
		{
66 21
			$destination = Util::realPath($download->getDestination());
67 21
			if (empty($destination))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
			{
69 1
				$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_DESTINATION');
70 1
				$download->setState(Download::STATE_DOWNLOADING_ERROR);
71 1
				$this->handleCommand(new SaveDownloads([
72 1
						$download
73
				]));
74
75 1
				continue;
76
			}
77
78 20
			$src = $this->getSourceAdapter($download);
79 20
			if (empty($src))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
			{
81 4
				$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_PATH');
82 4
				$download->setState(Download::STATE_DOWNLOADING_ERROR);
83 4
				$this->handleCommand(new SaveDownloads([
84 4
						$download
85
				]));
86 4
				continue;
87
			}
88
89 17
			$src = new Filesystem($src);
90 17
			$dest = new Filesystem(new Local($destination));
91
92 17
			$manager = $this->manager;
93 17
			$manager->mountFilesystem('src-' . $download->getId(), $src);
94 17
			$manager->mountFilesystem('dst-' . $download->getId(), $dest);
95
96 17
			$promise = new Promise(
97 17
					function  () use ( &$promise, $download, $manager) {
98 17
						$fileName = basename($download->getLink());
99 17
						$destFileName = $download->getFileName() ? $download->getFileName() : $fileName;
100 17
						$id = $download->getId();
101 17
						if (! @$manager->copy('src-' . $id . '://' . $fileName, 'dst-' . $id . '://' . $destFileName))
102
						{
103 9
							$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_COPY_FAILED');
104 9
							$download->setState(Download::STATE_DOWNLOADING_ERROR);
105
						}
106 8
						else if (! empty($download->getHash()) && $download->getHash() != md5_file(
107 2
								$manager->getFilesystem('dst-' . $id)
108 2
									->getAdapter()
109 8
									->applyPathPrefix($destFileName)))
110
						{
111 1
							$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_HASH');
112 1
							$download->setState(Download::STATE_DOWNLOADING_ERROR);
113
114 1
							if ($manager->has('dst-' . $id . '://' . $destFileName))
115
							{
116 1
								$manager->delete('dst-' . $id . '://' . $destFileName);
117
							}
118
						}
119
						else
120
						{
121 7
							$download->setState(Download::STATE_DOWNLOADING_COMPLETED);
122
						}
123 17
						$download->setFinishedAt(new \DateTime());
124
125 17
						$this->handleCommand(new SaveDownloads([
126 17
								$download
127
						]));
128
129 17
						$promise->resolve(true);
130 17
					});
131 17
			$promises[] = $promise;
132
		}
133
134 22
		return $promises;
135
	}
136
137
	/**
138
	 * Returns the source adapter to copy the download from.
139
	 * If none can be created null is returned.
140
	 *
141
	 * @param Download $download
142
	 * @return null|\League\Flysystem\AdapterInterface
143
	 */
144 12
	protected function getSourceAdapter (Download $download)
145
	{
146 12
		$uri = Util::parseUrl($download->getLink());
147
148 12
		$path = Util::realPath($uri['path']);
149 12
		if (empty($path))
150
		{
151
			// Perhaps relative
152 4
			$path = Util::realPath(ltrim($uri['path'], '/'));
153 4
			if (empty($path))
154
			{
155 3
				return null;
156
			}
157
		}
158
159 10
		return new Local(dirname($path));
160
	}
161
162 6
	protected function getConfiguration ()
163
	{
164 6
		return $this->configuration;
165
	}
166
}