Completed
Push — master ( 3a9f70...45da6a )
by C
05:33
created

Localhost::getConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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