Passed
Push — master ( bebb1c...5d6b43 )
by Milan
02:46
created

src/DI/UploadExtension.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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