Configuration   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createUrl() 0 17 3
1
<?php
2
/**
3
 * Configuration.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	common
10
 * @since		5.0
11
 *
12
 * @date		01.03.15
13
 */
14
15
namespace IPub\Twitter;
16
17
use Nette;
18
use Nette\Http;
19
20
/**
21
 * Twitter's extension configuration storage. Store basic extension settings
22
 *
23
 * @package		iPublikuj:Twitter!
24
 * @subpackage	common
25
 *
26
 * @author Adam Kadlec <[email protected]>
27
 */
28
class Configuration extends Nette\Object
29
{
30
	/**
31
	 * @var string
32
	 */
33
	public $consumerKey;
34
35
	/**
36
	 * @var string
37
	 */
38
	public $consumerSecret;
39
40
	/**
41
	 * @var array
42
	 */
43
	public $domains = [
44
		'oauth' => 'https://api.twitter.com/oauth/',
45
		'api' => 'https://api.twitter.com/1.1/',
46
		'upload' => 'https://upload.twitter.com/1.1/',
47
	];
48
49
	/**
50
	 * @param string $consumerKey
51
	 * @param string $consumerSecret
52
	 */
53
	public function __construct($consumerKey, $consumerSecret)
54
	{
55
		$this->consumerKey = $consumerKey;
56
		$this->consumerSecret = $consumerSecret;
57
	}
58
59
	/**
60
	 * Build the URL for given domain alias, path and parameters.
61
	 *
62
	 * @param string $name The name of the domain
63
	 * @param string $path Optional path (without a leading slash)
64
	 * @param array $params Optional query parameters
65
	 *
66
	 * @return Http\UrlScript The URL for the given parameters
67
	 */
68
	public function createUrl($name, $path = NULL, $params = [])
69
	{
70
		if (preg_match('~^https?://([^.]+\\.)?twitter\\.com/~', trim($path))) {
71
			$url = new Http\UrlScript($path);
72
73
		} else {
74
			$url = new Http\UrlScript($this->domains[$name]);
75
			$path = $url->getPath() . ltrim($path, '/');
76
			$url->setPath($path);
77
		}
78
79
		$url->appendQuery(array_map(function ($param) {
80
			return $param instanceof Http\UrlScript ? (string) $param : $param;
81
		}, $params));
82
83
		return $url;
84
	}
85
}