ConsoleExtension   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 97.73%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 35
c 3
b 1
f 0
dl 0
loc 86
ccs 43
cts 44
cp 0.9773
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigSchema() 0 6 1
A loadConfiguration() 0 6 2
A registerConsoleCommands() 0 8 2
A beforeCompile() 0 18 4
A registerHelpers() 0 7 2
A setupApplication() 0 13 3
1
<?php declare(strict_types = 1);
2
3
namespace Portiny\Console\Adapter\Nette\DI;
4
5
use Nette\DI\CompilerExtension;
6
use Nette\DI\Statement;
7
use Nette\Http\Request;
8
use Nette\Http\UrlScript;
9
use Nette\Schema\Expect;
10
use Nette\Schema\Schema;
11
use stdClass;
12
use Symfony\Component\Console\Application;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Helper\HelperInterface;
15
use Symfony\Component\Console\Helper\HelperSet;
16
17
class ConsoleExtension extends CompilerExtension
18
{
19
20 1
	public function getConfigSchema(): Schema
21
	{
22 1
		return Expect::structure([
23 1
			'url' => Expect::anyOf(Expect::string(), Expect::null()),
24 1
			'autoExit' => Expect::bool(),
25 1
			'catchExceptions' => Expect::bool(),
26
		]);
27
	}
28
29
30
	/**
31
	 * {@inheritdoc}
32
	 */
33 1
	public function loadConfiguration(): void
34
	{
35 1
		$config = $this->loadFromFile(__DIR__ . '/../config/config.neon');
36 1
		$this->loadDefinitionsFromConfig($config['services'] ?: []);
37
38 1
		$this->setupApplication();
39 1
	}
40
41
42
	/**
43
	 * {@inheritdoc}
44
	 */
45 1
	public function beforeCompile(): void
46
	{
47 1
		if (PHP_SAPI !== 'cli') {
48
			return;
49
		}
50
51
		/** @var stdClass $config */
52 1
		$config = (object) $this->config;
53 1
		$builder = $this->getContainerBuilder();
54 1
		$builder->getDefinitionByType(Application::class)
55 1
			->addSetup('setHelperSet', ['@' . $builder->getByType(HelperSet::class)]);
56
57 1
		$this->registerConsoleCommands();
58 1
		$this->registerHelpers();
59
60 1
		if ($config->url !== null && $builder->hasDefinition('http.request')) {
61 1
			$definition = $builder->getDefinition('http.request');
62 1
			$definition->setFactory(Request::class, [new Statement(UrlScript::class, [$config->url])]);
0 ignored issues
show
Unused Code introduced by
The call to Nette\DI\Statement::__construct() has too many arguments starting with Nette\Http\UrlScript::class. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
			$definition->setFactory(Request::class, [/** @scrutinizer ignore-call */ new Statement(UrlScript::class, [$config->url])]);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Deprecated Code introduced by
The class Nette\DI\Statement has been deprecated: use Nette\DI\Definitions\Statement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

62
			$definition->setFactory(Request::class, [/** @scrutinizer ignore-deprecated */ new Statement(UrlScript::class, [$config->url])]);
Loading history...
63
		}
64 1
	}
65
66
67 1
	private function setupApplication(): void
68
	{
69
		/** @var stdClass $config */
70 1
		$config = (object) $this->config;
71 1
		$builder = $this->getContainerBuilder();
72 1
		$applicationDefinition = $builder->getDefinitionByType(Application::class);
73
74 1
		if ($config->autoExit !== null) {
75 1
			$applicationDefinition->addSetup('setAutoExit', [(bool) $config->autoExit]);
76
		}
77
78 1
		if ($config->catchExceptions !== null) {
79 1
			$applicationDefinition->addSetup('setCatchExceptions', [(bool) $config->catchExceptions]);
80
		}
81 1
	}
82
83
84 1
	private function registerConsoleCommands(): void
85
	{
86 1
		$builder = $this->getContainerBuilder();
87 1
		$applicationDefinition = $builder->getDefinitionByType(Application::class);
88
89 1
		$commandDefinitions = $builder->findByType(Command::class);
90 1
		foreach (array_keys($commandDefinitions) as $name) {
91 1
			$applicationDefinition->addSetup('add', ['@' . $name]);
92
		}
93 1
	}
94
95
96 1
	private function registerHelpers(): void
97
	{
98 1
		$builder = $this->getContainerBuilder();
99 1
		$helperSetDefinition = $builder->getDefinitionByType(HelperSet::class);
100
101 1
		foreach ($builder->findByType(HelperInterface::class) as $helperDefinition) {
102 1
			$helperSetDefinition->addSetup('set', ['@' . $helperDefinition->getType(), null]);
103
		}
104 1
	}
105
106
}
107