FiveHundredPixelExtension::loadConfiguration()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 8.8571
cc 2
eloc 20
nc 2
nop 0
1
<?php
2
/**
3
 * FiveHundredPixelExtension.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:500px!
9
 * @subpackage	DI
10
 * @since		5.0
11
 *
12
 * @date		06.03.15
13
 */
14
15
namespace IPub\FiveHundredPixel\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\FiveHundredPixel;
26
27
class FiveHundredPixelExtension extends DI\CompilerExtension
28
{
29
	/**
30
	 * Extension default configuration
31
	 *
32
	 * @var array
33
	 */
34
	protected $defaults = [
35
		'consumerKey' => NULL,
36
		'consumerSecret' => NULL,
37
		'permission' => 'read',          // read/write/delete
38
		'clearAllWithLogout' => TRUE
39
	];
40
41
	public function loadConfiguration()
42
	{
43
		$config = $this->getConfig($this->defaults);
44
		$builder = $this->getContainerBuilder();
45
46
		Utils\Validators::assert($config['consumerKey'], 'string', 'Application key');
47
		Utils\Validators::assert($config['consumerSecret'], 'string', 'Application secret');
48
		Utils\Validators::assert($config['permission'], 'string', 'Application permission');
49
50
		// Create oAuth consumer
51
		$consumer = new IPub\OAuth\Consumer($config['consumerKey'], $config['consumerSecret']);
52
53
		$builder->addDefinition($this->prefix('client'))
54
			->setClass('IPub\FiveHundredPixel\Client', [$consumer]);
55
56
		$builder->addDefinition($this->prefix('config'))
57
			->setClass('IPub\FiveHundredPixel\Configuration', [
58
				$config['consumerKey'],
59
				$config['consumerSecret'],
60
			])
61
			->addSetup('$permission', [$config['permission']]);
62
63
		$builder->addDefinition($this->prefix('session'))
64
			->setClass('IPub\FiveHundredPixel\SessionStorage');
65
66
		if ($config['clearAllWithLogout']) {
67
			$builder->getDefinition('user')
68
				->addSetup('$sl = ?; ?->onLoggedOut[] = function () use ($sl) { $sl->getService(?)->clearAll(); }', array(
69
					'@container', '@self', $this->prefix('session')
70
				));
71
		}
72
	}
73
74
	/**
75
	 * @param Nette\Configurator $config
76
	 * @param string $extensionName
77
	 */
78
	public static function register(Nette\Configurator $config, $extensionName = '500px')
79
	{
80
		$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) {
81
			$compiler->addExtension($extensionName, new FiveHundredPixelExtension());
82
		};
83
	}
84
}