Completed
Push — master ( 33fb79...c738ff )
by Adam
12s queued 10s
created

EmailModuleExtension::loadConfiguration()   B

Complexity

Conditions 10
Paths 72

Size

Total Lines 61
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 36
c 2
b 0
f 0
dl 0
loc 61
ccs 0
cts 33
cp 0
rs 7.6666
cc 10
nc 72
nop 0
crap 110

How to fix   Long Method    Complexity   

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
 * EmailModuleExtension.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:EmailModule!
9
 * @subpackage     DI
10
 * @since          1.0.0
11
 *
12
 * @date           30.12.16
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\EmailModule\DI;
18
19
use Nette;
20
use Nette\DI;
21
22
use IPub\EmailModule;
23
use IPub\EmailModule\Entities;
24
use IPub\EmailModule\Mailer;
25
26
use IPub\Framework;
27
28
/**
29
 * Email module extension container
30
 *
31
 * @package        iPublikuj:EmailModule!
32
 * @subpackage     DI
33
 *
34
 * @author         Adam Kadlec <[email protected]>
35
 */
36
final class EmailModuleExtension extends Framework\DI\Extension
37
{
38
	/**
39
	 * Module default configuration
40
	 *
41
	 * @var array
42 1
	 */
43
	protected $defaults = [
44
		'from'       => [
45
			'address' => NULL,
46
			'name'    => NULL,
47
		],
48
		'replyTo'       => [
49
			'address' => NULL,
50
			'name'    => NULL,
51
		],
52
		'mailer'     => [
53
			'type'       => 'file',
54
			'port'       => NULL,
55
			'host'       => NULL,
56
			'username'   => NULL,
57
			'password'   => NULL,
58
			'encryption' => 'none',
59
			'folder'     => NULL,
60
		],
61
		'parameters' => [
62
			'layouts'    => '@@default',
63
			'siteUrl'    => 'https://www.ipublikuj.eu',
64
			'siteName'   => 'iPublikuj:cms!',
65
			'senderName' => 'iPublikuj:cms! team',
66
			'signature'  => '',
67
		],
68
	];
69
70
	/**
71
	 * {@inheritdoc}
72
	 */
73
	public function loadConfiguration() : void
74
	{
75
		parent::loadConfiguration();
76
77
		// Get container builder
78
		$builder = $this->getContainerBuilder();
79
		// Get extension configuration
80
		$configuration = $this->getConfig();
81
82
		// In debug mode turn on logger
83
		if ($configuration['mailer']['type'] === 'file') {
84
			$mailers = $builder->findByType(Nette\Mail\IMailer::class);
85
86
			foreach ($mailers as $mailer) {
87
				$mailer->setAutowired(FALSE);
88
			}
89
90
			$builder->addDefinition($this->prefix('fileMailer'))
91
				->setType(Mailer\FileMailer::class)
92
				->setArguments([$configuration['mailer']['folder']])
93
				->setAutowired(TRUE);
94
		}
95
96
		// Enable SMTP mailer
97
		if ($configuration['mailer']['type'] === 'smtp') {
98
			$builder->addDefinition($this->prefix('smtpMailer'))
99
				->setType(Nette\Mail\SmtpMailer::class)
100
				->setArguments([
101
					'host'     => $configuration['mailer']['host'],
102
					'port'     => $configuration['mailer']['port'],
103
					'username' => $configuration['mailer']['username'],
104
					'password' => $configuration['mailer']['password'],
105
					'secure'   => (in_array($configuration['mailer']['encryption'], ['ssl', 'tls']) ? $configuration['mailer']['encryption'] : ''),
106
				])
107
				->setAutowired(TRUE);
108
		}
109
110
		/** @var DI\Definitions\ServiceDefinition $emailFactory */
111
		$emailFactory = $builder->getDefinition($builder->getByType(EmailModule\Email\IEmailFactory::class));
0 ignored issues
show
Bug introduced by Adam Kadlec
It seems like $builder->getByType(IPub...l\IEmailFactory::class) can also be of type null; however, parameter $name of Nette\DI\ContainerBuilder::getDefinition() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

111
		$emailFactory = $builder->getDefinition(/** @scrutinizer ignore-type */ $builder->getByType(EmailModule\Email\IEmailFactory::class));
Loading history...
112
		$emailFactoryDefinition = $emailFactory->getResultDefinition();
0 ignored issues
show
Bug introduced by Adam Kadlec
Are you sure the assignment to $emailFactoryDefinition is correct as $emailFactory->getResultDefinition() targeting Nette\DI\Definitions\ServiceDefinition::__call() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
113
114
		foreach ($configuration['parameters'] as $key => $value) {
115
			$emailFactoryDefinition->addSetup('?->setParameterItem(?, ?)', [$emailFactory, $key, $value]);
116
		}
117
118
		$messageFactory = $builder->getDefinition($builder->getByType(EmailModule\Message\IMessageFactory::class));
119
120
		if ($configuration['from']['address']) {
121
			$name = $configuration['from']['name'] ? $configuration['from']['name'] : '';
122
			$defaultSender = new Entities\Addresses\Address($configuration['from']['address'], (string) $name);
123
124
			$messageFactory->getResultDefinition()
0 ignored issues
show
Bug introduced by Adam Kadlec
Are you sure the usage of $messageFactory->getResultDefinition() targeting Nette\DI\Definitions\Definition::__call() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
125
				->setArgument('from', $defaultSender);
126
		}
127
128
		if ($configuration['replyTo']['address']) {
129
			$name = $configuration['replyTo']['name'] ? $configuration['replyTo']['name'] : '';
130
			$replyTo = new Entities\Addresses\Address($configuration['replyTo']['address'], (string) $name);
131
132
			$messageFactory->getResultDefinition()
0 ignored issues
show
Bug introduced by Adam Kadlec
Are you sure the usage of $messageFactory->getResultDefinition() targeting Nette\DI\Definitions\Definition::__call() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
133
				->setArgument('replyTo', $replyTo);
134
		}
135
	}
136
137
	/**
138
	 * @param Nette\Configurator $config
139
	 * @param string $extensionName
140
	 *
141
	 * @return void
142
	 */
143
	public static function register(
144
		Nette\Configurator $config,
145
		string $extensionName = 'ipub/email-module'
146
	) : void {
147
		$config->onCompile[] = function (Nette\Configurator $config, DI\Compiler $compiler) use ($extensionName) {
148
			$compiler->addExtension($extensionName, new EmailModuleExtension);
149
		};
150
	}
151
}
152