Completed
Push — master ( 054527...be0150 )
by Milan
01:52
created

UploadExtension::loadConfiguration()   C

Complexity

Conditions 9
Paths 36

Size

Total Lines 59
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 6.9133
c 0
b 0
f 0
cc 9
eloc 40
nc 36
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 Nette\DI;
6
7
class UploadExtension extends DI\CompilerExtension
8
{
9
	private $defaults = [
10
		'ftp' => [],
11
		'destinations' => '%wwwDir%/upload',
12
	];
13
14
	private $ftp = [
15
		'host' => '',
16
		'user' => '',
17
		'password' => '',
18
		'url' => '',
19
		'path' => '',
20
		'port' => 21,
21
		'passive' => true,
22
	];
23
24
	public function loadConfiguration()
25
	{
26
		$this->config += $this->defaults;
27
		$builder = $this->getContainerBuilder();
28
		$config = DI\Helpers::expand($this->config, $builder->parameters);
29
30
		if (!is_array($config['destinations'])) {
31
			if ($config['destinations']) {
32
				$config['destinations'] = ['default' => $config['destinations']];
33
			} else {
34
				$config['destinations'] = [];
35
			}
36
		}
37
38
		foreach ($config['ftp'] as $name => $options) {
39
			$options += $this->ftp;
40
			$ftp = $builder->addDefinition($this->prefix('ftp.' . $name))
41
				->setFactory('Ftp')
42
				->setAutowired(false)
43
				->addSetup('connect', [$options['host'], $options['port']])
44
				->addSetup('login', [$options['user'], $options['password']])
45
				->addSetup('pasv', [$options['passive']]);
46
			if ($options['path']) {
47
				$ftp->addSetup('chdir', [$options['path']]);
48
			}
49
50
			$config['destinations'][$name] = $builder->addDefinition($this->prefix('driver.' . $name))
51
				->setFactory('h4kuna\Upload\Driver\Ftp', [$options['url'], $ftp]);
52
		}
53
54
		// Filename
55
		$filename = $builder->addDefinition($this->prefix('filename'))
56
			->setFactory('h4kuna\Upload\Store\Filename');
57
58
		$autowired = true;
59
		foreach ($config['destinations'] as $info => $destination) {
60
			if (is_string($destination) && is_dir($destination)) {
61
				$definition = $builder->addDefinition($this->prefix('driver.' . $info))
62
					->setFactory('h4kuna\Upload\Driver\LocalFilesystem', [$destination])
63
					->setAutowired($autowired);
64
			} 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...
65
				$definition = $destination->setAutowired($autowired);
66
			} else {
67
				$definition = $destination;
68
			}
69
70
			// Download
71
			$builder->addDefinition($this->prefix('download.' . $info))
72
				->setFactory('h4kuna\Upload\Download', [$definition, '@http.request', '@http.response'])
73
				->setAutowired($autowired);
74
75
			// Upload
76
			$builder->addDefinition($this->prefix('upload.' . $info))
77
				->setFactory('h4kuna\Upload\Upload', [$info, $definition, $filename])
78
				->setAutowired($autowired);
79
80
			$autowired = false;
81
		}
82
	}
83
}
84