UrlGenerator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 130
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A asset_url() 0 11 1
A base_url() 0 8 2
B url() 0 25 3
A default_url() 0 12 2
A full_url() 0 19 3
1
<?php declare(strict_types=1);
2
/**
3
 * Hummingbird Anime Client
4
 *
5
 * An API client for Hummingbird to manage anime and manga watch lists
6
 *
7
 * PHP version 7
8
 *
9
 * @package     HummingbirdAnimeClient
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2015 - 2016  Timothy J. Warren
12
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13
 * @version     3.1
14
 * @link        https://github.com/timw4mail/HummingBirdAnimeClient
15
 */
16
17
namespace Aviat\AnimeClient;
18
19
use Aviat\Ion\Di\ContainerInterface;
20
21
/**
22
 * UrlGenerator class.
23
 */
24
class UrlGenerator extends RoutingBase {
25
26
	/**
27
	 * The current HTTP host
28
	 * @var string
29
	 */
30
	protected $host;
31
32
	/**
33
	 * Constructor
34
	 *
35
	 * @param ContainerInterface $container
36
	 */
37
	public function __construct(ContainerInterface $container)
38
	{
39
		parent::__construct($container);
40
		$this->host = $container->get('request')->getServerParams()['HTTP_HOST'];
41
	}
42
43
	/**
44
	 * Get the base url for css/js/images
45
	 *
46
	 * @return string
47
	 */
48
	public function asset_url()
49
	{
50
		$args = func_get_args();
51
		$base_url = rtrim($this->url(""), '/');
52
53
		$base_url = "{$base_url}" . $this->__get("asset_path");
54
55
		array_unshift($args, $base_url);
56
57
		return implode("/", $args);
58
	}
59
60
	/**
61
	 * Get the base url from the config
62
	 *
63
	 * @param string $type - (optional) The controller
64
	 * @return string
65
	 */
66
	public function base_url($type = "anime")
67
	{
68
		$config_path = trim($this->__get("{$type}_path"), "/");
69
70
		$path = ($config_path !== '') ? $config_path : "";
71
72
		return implode("/", ['/', $this->host, $path]);
73
	}
74
75
	/**
76
	 * Generate a proper url from the path
77
	 *
78
	 * @param string $path
79
	 * @return string
80
	 */
81
	public function url($path)
82
	{
83
		$path = trim($path, '/');
84
85
		$path = preg_replace('`{/.*?}`i', '', $path);
86
87
		// Remove any optional parameters from the route
88
		// and replace them with existing route parameters, if they exist
89
		$path_segments = explode('/', $path);
90
		$segment_count = count($path_segments);
91
		$segments = $this->segments();
92
93
		for ($i = 0; $i < $segment_count; $i++)
94
		{
95
			if ( ! array_key_exists($i + 1, $segments))
96
			{
97
				$segments[$i + 1] = "";
98
			}
99
100
			$path_segments[$i] = preg_replace('`{.*?}`i', $segments[$i + 1], $path_segments[$i]);
101
		}
102
		$path = implode('/', $path_segments);
103
104
		return "//{$this->host}/{$path}";
105
	}
106
107
	/**
108
	 * Full default path for the list pages
109
	 *
110
	 * @param string $type
111
	 * @throws \InvalidArgumentException
112
	 * @return string
113
	 */
114
	public function default_url($type)
115
	{
116
		$type = trim($type);
117
		$default_path = $this->__get("default_{$type}_list_path");
118
119
		if ( ! is_null($default_path))
120
		{
121
			return $this->url("{$type}/{$default_path}");
122
		}
123
124
		throw new \InvalidArgumentException("Invalid default type: '{$type}'");
125
	}
126
127
	/**
128
	 * Generate full url path from the route path based on config
129
	 *
130
	 * @param string $path - (optional) The route path
131
	 * @param string $type - (optional) The controller (anime or manga), defaults to anime
132
	 * @return string
133
	 */
134
	public function full_url($path = "", $type = "anime")
135
	{
136
		$config_default_route = $this->__get("default_{$type}_path");
137
138
		// Remove beginning/trailing slashes
139
		$path = trim($path, '/');
140
141
		// Set the default view
142
		if ($path === '')
143
		{
144
			$path .= trim($config_default_route, '/');
145
			if ($this->__get('default_to_list_view'))
146
			{
147
				$path .= '/list';
148
			}
149
		}
150
151
		return $this->url($path);
152
	}
153
}
154
// End of UrlGenerator.php