Configuration::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
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:500px!
9
 * @subpackage	common
10
 * @since		5.0
11
 *
12
 * @date		07.03.15
13
 */
14
15
namespace IPub\FiveHundredPixel;
16
17
use Nette;
18
use Nette\Http;
19
20
/**
21
 * 500px's extension configuration storage. Store basic extension settings
22
 *
23
 * @package		iPublikuj:500px!
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 string
42
	 */
43
	public $permission;
44
45
	/**
46
	 * @var array
47
	 */
48
	public $domains = [
49
		'oauth' => 'https://api.500px.com/v1/oauth/',
50
		'api' => 'https://api.500px.com/v1/',
51
		'upload' => 'http://upload.500px.com/v1/',
52
	];
53
54
	/**
55
	 * @param string $consumerKey
56
	 * @param string $consumerSecret
57
	 */
58
	public function __construct($consumerKey, $consumerSecret)
59
	{
60
		$this->consumerKey = $consumerKey;
61
		$this->consumerSecret = $consumerSecret;
62
	}
63
64
	/**
65
	 * Build the URL for given domain alias, path and parameters.
66
	 *
67
	 * @param string $name The name of the domain
68
	 * @param string $path Optional path (without a leading slash)
69
	 * @param array $params Optional query parameters
70
	 *
71
	 * @return Http\UrlScript The URL for the given parameters
72
	 */
73
	public function createUrl($name, $path = NULL, $params = [])
74
	{
75
		if (preg_match('~^https?://([^.]+\\.)?fiveHundredPixel\\.com/~', trim($path))) {
76
			$url = new Http\UrlScript($path);
77
78
		} else {
79
			$url = new Http\UrlScript($this->domains[$name]);
80
			$path = $url->getPath() . ltrim($path, '/');
81
			$url->setPath($path);
82
		}
83
84
		$url->appendQuery(array_map(function ($param) {
85
			return $param instanceof Http\UrlScript ? (string) $param : $param;
86
		}, $params));
87
88
		return $url;
89
	}
90
}