SMSenderExtension::beforeCompile()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 15
cts 15
cp 1
rs 9.52
c 0
b 0
f 0
cc 4
nc 6
nop 0
crap 4
1
<?php
2
3
namespace RM\SMSender\DI;
4
5
use ReflectionClass;
6
use Nette\Configurator;
7
use Nette\DI\Compiler;
8
use Nette\DI\CompilerExtension;
9
use Nette\DI\Helpers;
10
11
/**
12
 * Nette DI extension for SMSender.
13
 */
14 1
class SMSenderExtension extends CompilerExtension
15
{
16
	/** @var array */
17
	public $defaults = [
18
		'config' => [],
19
		'senderClass' => 'RM\SMSender\EuroSms\Sender',
20
		'setDebugMode' => FALSE,
21
		'messageClass' => 'RM\SMSender\EuroSms\Message',
22
		'messageFactoryClass' => 'RM\SMSender\MessageFactory',
23
		'message' => [
24
			'setFrom' => NULL,
25
			'signature' => NULL,
26
		],
27
	];
28
29
	public function beforeCompile()
30
	{
31 1
		$builder = $this->getContainerBuilder();
32
33 1
		$config = $this->validateConfig($this->defaults);
0 ignored issues
show
Deprecated Code introduced by
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...
34
35 1
		$sender = $builder->addDefinition($this->prefix('sender'))
0 ignored issues
show
Deprecated Code introduced by
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...
36 1
			->setClass($config['senderClass']);
37
38 1
		foreach ($config as $method => $value) {
39 1
			$tmp = new $config['senderClass'];
40 1
			if ((new ReflectionClass($tmp))->hasMethod($method)) {
41 1
				$sender->addSetup($method, [$value]);
42
			}
43
		}
44
45 1
		if ($config['message']['signature']) {
46 1
			$sender->addSetup('$service->onBeforeSend[] = function ($message) { $message->setText($message->getText() . ?); };', [$config['message']['signature']]);
47
		}
48
49 1
		$builder->addDefinition($this->prefix('messageFactory'))
0 ignored issues
show
Deprecated Code introduced by
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...
50 1
			->setClass($config['messageFactoryClass'])
51 1
			->addSetup('$class', [$config['messageClass']])
52 1
			->addSetup('$params', [$config['message']]);
53 1
	}
54
55
	/**
56
	 * Register extension to DI Container.
57
	 * @param  Configurator $config
58
	 */
59
	public static function register(Configurator $config)
60
	{
61 1
		$config->onCompile[] = function (Configurator $config, Compiler $compiler) {
62 1
			$compiler->addExtension('smsender', new SMSenderExtension());
63 1
		};
64 1
	}
65
}
66