Passed
Push — master ( feec22...d030f3 )
by Milan
01:50
created

UploadExtension   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 79
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C loadConfiguration() 0 53 9
1
<?php
2
3
namespace h4kuna\Upload\DI;
4
5
use Nette\DI;
6
7
class UploadExtension extends DI\CompilerExtension
8
{
9
10
	private $defaults = [
11
		'ftp' => [],
12
		'destinations' => '',
13
	];
14
15
	private $ftp = [
16
		'host' => '',
17
		'user' => '',
18
		'password' => '',
19
		'url' => '',
20
		'path' => '',
21
		'port' => 21,
22
		'passive' => true,
23
	];
24
25
26
	public function __construct($wwwDir = null)
27
	{
28
		$this->defaults['destinations'] = $wwwDir . DIRECTORY_SEPARATOR . 'upload';
29
	}
30
31
32
	public function loadConfiguration()
33
	{
34
		$builder = $this->getContainerBuilder();
35
		$config = $this->config + $this->defaults;;
36
		if (!is_array($config['destinations'])) {
37
			if ($config['destinations']) {
38
				$config['destinations'] = ['default' => $config['destinations']];
39
			} else {
40
				$config['destinations'] = [];
41
			}
42
		}
43
44
		foreach ($config['ftp'] as $name => $options) {
45
			$options += $this->ftp;
46
			$ftp = $builder->addDefinition($this->prefix('ftp.' . $name))
47
				->setFactory('Ftp')
48
				->setAutowired(false)
49
				->addSetup('connect', [$options['host'], $options['port']])
50
				->addSetup('login', [$options['user'], $options['password']])
51
				->addSetup('pasv', [$options['passive']]);
52
			if ($options['path']) {
53
				$ftp->addSetup('chdir', [$options['path']]);
54
			}
55
56
			$config['destinations'][$name] = $builder->addDefinition($this->prefix('driver.' . $name))
57
				->setFactory('h4kuna\Upload\Driver\Ftp', [$options['url'], $ftp]);
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('h4kuna\Upload\Driver\LocalFilesystem', [$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('h4kuna\Upload\Download', [$definition, '@http.request', '@http.response'])
75
				->setAutowired($autowired);
76
77
			// Upload
78
			$builder->addDefinition($this->prefix('upload.' . $info))
79
				->setFactory('h4kuna\Upload\Upload', [$definition])
80
				->setAutowired($autowired);
81
82
			$autowired = false;
83
		}
84
	}
85
}
86