TwitterExtension   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 3
c 5
b 0
f 0
lcom 1
cbo 7
dl 0
loc 55
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B loadConfiguration() 0 30 2
A register() 0 6 1
1
<?php
2
/**
3
 * TwitterExtension.php
4
 *
5
 * @copyright	More in license.md
6
 * @license		http://www.ipublikuj.eu
7
 * @author		Adam Kadlec http://www.ipublikuj.eu
8
 * @package		iPublikuj:Twitter!
9
 * @subpackage	DI
10
 * @since		5.0
11
 *
12
 * @date		24.05.13
13
 */
14
15
namespace IPub\Twitter\DI;
16
17
use Nette;
18
use Nette\DI;
19
use Nette\Utils;
20
use Nette\PhpGenerator as Code;
21
22
use Tracy;
23
24
use IPub;
25
use IPub\Twitter;
26
27
class TwitterExtension extends DI\CompilerExtension
28
{
29
	/**
30
	 * Extension default configuration
31
	 *
32
	 * @var array
33
	 */
34
	protected $defaults = [
35
		'consumerKey' => NULL,
36
		'consumerSecret' => NULL,
37
		'clearAllWithLogout' => TRUE,
38
	];
39
40
	public function loadConfiguration()
41
	{
42
		$config = $this->getConfig($this->defaults);
43
		$builder = $this->getContainerBuilder();
44
45
		Utils\Validators::assert($config['consumerKey'], 'string', 'Application key');
46
		Utils\Validators::assert($config['consumerSecret'], 'string', 'Application secret');
47
48
		// Create oAuth consumer
49
		$consumer = new IPub\OAuth\Consumer($config['consumerKey'], $config['consumerSecret']);
50
51
		$builder->addDefinition($this->prefix('client'))
52
			->setClass('IPub\Twitter\Client', [$consumer]);
53
54
		$builder->addDefinition($this->prefix('config'))
55
			->setClass('IPub\Twitter\Configuration', [
56
				$config['consumerKey'],
57
				$config['consumerSecret'],
58
			]);
59
60
		$builder->addDefinition($this->prefix('session'))
61
			->setClass('IPub\Twitter\SessionStorage');
62
63
		if ($config['clearAllWithLogout']) {
64
			$builder->getDefinition('user')
65
				->addSetup('$sl = ?; ?->onLoggedOut[] = function () use ($sl) { $sl->getService(?)->clearAll(); }', [
66
					'@container', '@self', $this->prefix('session')
67
				]);
68
		}
69
	}
70
71
	/**
72
	 * @param Nette\Configurator $config
73
	 * @param string $extensionName
74
	 */
75
	public static function register(Nette\Configurator $config, $extensionName = 'twitter')
76
	{
77
		$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) {
78
			$compiler->addExtension($extensionName, new TwitterExtension());
79
		};
80
	}
81
}