Completed
Push — master ( b7789b...80e9ab )
by Milan
01:49
created

UploadExtension::loadConfiguration()   B

Complexity

Conditions 8
Paths 18

Size

Total Lines 56
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 56
rs 7.3333
cc 8
eloc 37
nc 18
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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' => '', // can be array
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
		if ($destinations !== null) {
31
			$this->defaults['destinations'] = $this->prepareDestination($destinations);
32
		}
33
	}
34
35
36
	public function loadConfiguration()
37
	{
38
		$builder = $this->getContainerBuilder();
39
		$config = $this->validateConfig($this->defaults);
40
41
		$config['destinations'] = $this->prepareDestination($config['destinations']);
42
43
		foreach ($config['ftp'] as $name => $options) {
44
			$options += $this->ftp;
45
			$ftp = $builder->addDefinition($this->prefix('ftp.' . $name))
46
				->setFactory('Ftp')
47
				->setAutowired(false)
48
				->addSetup('connect', [$options['host'], $options['port']])
49
				->addSetup('login', [$options['user'], $options['password']])
50
				->addSetup('pasv', [$options['passive']]);
51
			if ($options['path']) {
52
				$ftp->addSetup('chdir', [$options['path']]);
53
			}
54
55
			$config['destinations'][$name] = $builder->addDefinition($this->prefix('driver.' . $name))
56
				->setFactory(Driver\Ftp::class, [$options['url'], $ftp]);
57
		}
58
59
		if (!$config['destinations']) {
60
			throw new Upload\InvalidStateException('Destinations are empty! Fill it like path where to save files.');
61
		}
62
63
		$autowired = true;
64
		foreach ($config['destinations'] as $info => $destination) {
65
			if (is_string($destination)) {
66
				if (!is_dir($destination)) {
67
					throw new Upload\InvalidStateException('Create writable directory: "' . $destination . '"');
68
				}
69
70
				$definition = $builder->addDefinition($this->prefix('driver.' . $info))
71
					->setFactory(Driver\LocalFilesystem::class, [$destination])
72
					->setAutowired($autowired);
73
			} 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...
74
				$definition = $destination->setAutowired($autowired);
75
			} else {
76
				$definition = $destination;
77
			}
78
79
			// Download
80
			$builder->addDefinition($this->prefix('download.' . $info))
81
				->setFactory(Upload\Download::class, [$definition, '@http.request', '@http.response'])
82
				->setAutowired($autowired);
83
84
			// Upload
85
			$builder->addDefinition($this->prefix('upload.' . $info))
86
				->setFactory(Upload\Upload::class, [$definition])
87
				->setAutowired($autowired);
88
89
			$autowired = false;
90
		}
91
	}
92
93
94
	private function prepareDestination($destination)
95
	{
96
		if (is_array($destination)) {
97
			return $destination;
98
		}
99
100
		if (!$destination) {
101
			if ($this->compiler === NULL || !isset($this->getContainerBuilder()->parameters['wwwDir'])) {
102
				return [];
103
			}
104
105
			$destination = $this->getContainerBuilder()->parameters;['wwwDir'] . '/upload';
106
		}
107
		return ['default' => $destination];
108
	}
109
}
110