Completed
Push — master ( f090a3...8a44b2 )
by C
05:56
created

Localhost::getFileName()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 9.0534
cc 4
eloc 10
nc 4
nop 1
crap 4
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\AdapterInterface;
7
use League\Flysystem\Filesystem;
8
use League\Flysystem\MountManager;
9
use Tartana\Domain\Command\SaveDownloads;
10
use Tartana\Entity\Download;
11
use Tartana\Mixins\CommandBusAwareTrait;
12
use Tartana\Mixins\LoggerAwareTrait;
13
use Tartana\Util;
14
15
class Localhost implements HostInterface
16
{
17
	use LoggerAwareTrait;
18
	use CommandBusAwareTrait;
19
20 14
	private $configuration = null;
21
22 14
	private $manager = null;
23 14
24
	public function __construct (Registry $configuration = null, MountManager $manager = null)
25 3
	{
26
		$this->configuration = $configuration;
27 3
28
		if (empty($manager))
29 3
		{
30
			$manager = new MountManager();
31 1
		}
32
		$this->manager = $manager;
33
	}
34 2
35 2
	public function fetchDownloadInfo (array $downloads)
36
	{
37 1
		foreach ($downloads as $download)
38 1
		{
39
			if ($download->getFileName())
40
			{
41 1
				continue;
42 1
			}
43
44 3
			$fs = $this->getSourceAdapter($download);
45
			if (! empty($fs))
46 10
			{
47
				$download->setFileName(basename($download->getLink()));
48 10
			}
49
			else
50 9
			{
51 9
				$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_PATH');
52
				$download->setState(Download::STATE_DOWNLOADING_ERROR);
53 1
			}
54 1
		}
55
	}
56
57 8
	public function download (array $downloads)
58 8
	{
59
		$promises = [];
60 2
		foreach ($downloads as $download)
61 2
		{
62
			$destination = Util::realPath($download->getDestination());
63 View Code Duplication
			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...
64 7
			{
65 7
				$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_DESTINATION');
66
				$download->setState(Download::STATE_DOWNLOADING_ERROR);
67 7
				$this->handleCommand(new SaveDownloads([
68 7
						$download
69
				]));
70 7
71 7
				continue;
72 7
			}
73
74
			$src = $this->getSourceAdapter($download);
75 7 View Code Duplication
			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...
76
			{
77 1
				$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_PATH');
78
				$download->setState(Download::STATE_DOWNLOADING_ERROR);
79
				$this->handleCommand(new SaveDownloads([
80
						$download
81 6
				]));
82 7
				continue;
83
			}
84
85
			$src = new Filesystem($src);
86 10
			$dest = new Filesystem(new Local($destination));
87
88
			$manager = $this->manager;
89 10
			$manager->mountFilesystem('src-' . $download->getId(), $src);
90
			$manager->mountFilesystem('dst-' . $download->getId(), $dest);
91 10
92 10
			$promise = new Promise(
93
					function  () use ( &$promise, $download, $manager) {
94 1
						$fileName = basename($download->getLink());
95
						$id = $download->getId();
96
						if (! @$manager->copy('src-' . $id . '://' . $fileName,
97 9
								'dst-' . $id . '://' . ($download->getFileName() ? $download->getFileName() : $fileName)))
98 9
						{
99
							$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_COPY_FAILED');
100
							$download->setState(Download::STATE_DOWNLOADING_ERROR);
101 3
						}
102 3
						else
103
						{
104 2
							$download->setState(Download::STATE_DOWNLOADING_COMPLETED);
105
							$download->setFinishedAt(new \DateTime());
106
						}
107
108 8
						$this->handleCommand(new SaveDownloads([
109
								$download
110
						]));
111 10
112
						$promise->resolve(true);
113 10
					});
114 10
			$promises[] = $promise;
115 10
		}
116 10
117
		return $promises;
118 10
	}
119
120
	/**
121
	 * Returns the source adapter to copy the download from.
122
	 * If none can be created null is returned.
123
	 *
124
	 * @param Download $download
125
	 * @return null|AdapterInterface
126
	 */
127
	protected function getSourceAdapter (Download $download)
128
	{
129
		$uri = Util::parseUrl($download->getLink());
130
131
		$path = Util::realPath($uri['path']);
132
		if (empty($path))
133
		{
134
			// Perhaps relative
135
			$path = Util::realPath(ltrim($uri['path'], '/'));
136
			if (empty($path))
137
			{
138
				return null;
139
			}
140
		}
141
142
		return new Local(dirname($path));
143
	}
144
145
	protected function getConfiguration ()
146
	{
147
		return $this->configuration;
148
	}
149
}