MailingExtension   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 0
loc 51
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfiguration() 0 14 1
A expandConfigParams() 0 16 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
7
 * @author      Pavel Janda <[email protected]>
8
 * @package     Ublaboo
9
 */
10
11
namespace Ublaboo\Mailing\DI;
12
13
use Nette\DI\CompilerExtension;
14
use Nette\DI\Helpers;
15
use Ublaboo\Mailing\MailFactory;
16
use Ublaboo\Mailing\MailLogger;
17
18
class MailingExtension extends CompilerExtension
19
{
20
21
	public const CONFIG_LOG  = 'log';
22
	public const CONFIG_SEND = 'send';
23
	public const CONFIG_BOTH = 'both';
24
25
	/**
26
	 * @var array
27
	 */
28
	private $defaults = [
29
		'do' => self::CONFIG_BOTH,
30
		'logDirectory' => '%appDir%/../log/mails',
31
		'mailImagesBasePath' => '%wwwDir%',
32
		'mails' => [],
33
	];
34
35
36
	public function loadConfiguration(): void
37
	{
38
		$config = $this->expandConfigParams();
39
40
		$builder = $this->getContainerBuilder();
41
42
		$builder->addDefinition($this->prefix('mailLogger'))
0 ignored issues
show
Deprecated Code introduced by Scrutinizer Auto-Fixer
The method Nette\DI\Definitions\ServiceDefinition::setClass() has been deprecated with message: Use setType()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
43
			->setClass(MailLogger::class)
44
			->setArguments([$config['logDirectory']]);
45
46
		$builder->addDefinition($this->prefix('mailFactory'))
0 ignored issues
show
Deprecated Code introduced by Scrutinizer Auto-Fixer
The method Nette\DI\Definitions\ServiceDefinition::setClass() has been deprecated with message: Use setType()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
47
			->setClass(MailFactory::class)
48
			->setArguments([$config['do'], $config['mailImagesBasePath'], $config['mails']]);
49
	}
50
51
52
	private function expandConfigParams(): array
53
	{
54
		$config = $this->validateConfig($this->defaults, $this->config);
0 ignored issues
show
Bug introduced by Scrutinizer Auto-Fixer
It seems like $this->config can also be of type object; however, Nette\DI\CompilerExtension::validateConfig() does only seem to accept null|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Deprecated Code introduced by Scrutinizer Auto-Fixer
The method Nette\DI\CompilerExtension::validateConfig() has been deprecated with message: use getConfigSchema()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
55
56
		$config['logDirectory'] = Helpers::expand(
57
			$config['logDirectory'],
58
			$this->getContainerBuilder()->parameters
59
		);
60
61
		$config['mailImagesBasePath'] = Helpers::expand(
62
			$config['mailImagesBasePath'],
63
			$this->getContainerBuilder()->parameters
64
		);
65
66
		return $config;
67
	}
68
}
69