Passed
Push — master ( 80661e...c8031b )
by Milan
02:11
created

UploadExtension::prepareDestination()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace h4kuna\Upload\DI;
4
5
use h4kuna\Upload;
6
use h4kuna\Upload\Driver;
7
use Nette\DI;
8
9
class UploadExtension extends DI\CompilerExtension
10
{
11
12
	private $defaults = [
13
		'ftp' => [],
14
		'destinations' => [],
15
	];
16
17
	private $ftp = [
18
		'host' => '',
19
		'user' => '',
20
		'password' => '',
21
		'url' => '',
22
		'path' => '',
23
		'port' => 21,
24
		'passive' => true,
25
	];
26
27
28
	public function __construct($destinations = null)
29
	{
30
		$this->defaults['destinations'] = self::prepareDestination($destinations);
31
	}
32
33
34
	public function loadConfiguration()
35
	{
36
		$builder = $this->getContainerBuilder();
37
		$config = $this->config + $this->defaults;;
38
		$config['destinations'] = self::prepareDestination($config['destinations']);
39
40
		foreach ($config['ftp'] as $name => $options) {
41
			$options += $this->ftp;
42
			$ftp = $builder->addDefinition($this->prefix('ftp.' . $name))
43
				->setFactory('Ftp')
44
				->setAutowired(false)
45
				->addSetup('connect', [$options['host'], $options['port']])
46
				->addSetup('login', [$options['user'], $options['password']])
47
				->addSetup('pasv', [$options['passive']]);
48
			if ($options['path']) {
49
				$ftp->addSetup('chdir', [$options['path']]);
50
			}
51
52
			$config['destinations'][$name] = $builder->addDefinition($this->prefix('driver.' . $name))
53
				->setFactory(Driver\Ftp::class, [$options['url'], $ftp]);
54
		}
55
56
		if (!$config['destinations']) {
57
			throw new Upload\InvalidStateException('Destinations are empty! Fill it like path where to save files.');
58
		}
59
60
		$autowired = true;
61
		foreach ($config['destinations'] as $info => $destination) {
62
			if (is_string($destination) && is_dir($destination)) {
63
				$definition = $builder->addDefinition($this->prefix('driver.' . $info))
64
					->setFactory(Driver\LocalFilesystem::class, [$destination])
65
					->setAutowired($autowired);
66
			} elseif ($destination instanceof DI\ServiceDefinition) {
0 ignored issues
show
Bug introduced by
The class Nette\DI\ServiceDefinition does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
67
				$definition = $destination->setAutowired($autowired);
68
			} else {
69
				$definition = $destination;
70
			}
71
72
			// Download
73
			$builder->addDefinition($this->prefix('download.' . $info))
74
				->setFactory(Upload\Download::class, [$definition, '@http.request', '@http.response'])
75
				->setAutowired($autowired);
76
77
			// Upload
78
			$builder->addDefinition($this->prefix('upload.' . $info))
79
				->setFactory(Upload\Upload::class, [$definition])
80
				->setAutowired($autowired);
81
82
			$autowired = false;
83
		}
84
	}
85
86
87
	private static function prepareDestination($destination)
88
	{
89
		if (!is_array($destination)) {
90
			if ($destination) {
91
				return ['default' => $destination];
92
			}
93
			return [];
94
		}
95
		return $destination;
96
	}
97
}
98