Completed
Push — 3.2.x ( b7c6cd...c8027d )
by Erwan
02:05
created

categories_loader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B load() 0 35 2
A supports() 0 4 1
1
<?php
2
/**
3
 *
4
 * Pages extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace ernadoo\phpbbdirectory\routing;
12
13
use phpbb\db\driver\driver_interface;
14
use Symfony\Component\Config\Loader\Loader;
15
use Symfony\Component\Routing\Route;
16
use Symfony\Component\Routing\RouteCollection;
17
18
/**
19
* Loads routes defined in Page's database.
20
*/
21
class categories_loader extends Loader
22
{
23
	/** @var driver_interface */
24
	protected $db;
25
26
	/** @var string */
27
	protected $pages_table;
28
29
	/**
30
	* Constructor
31
	*
32
	* @param \phpbb\db\driver\driver_interface    $db          Database connection
33
	* @param string                               $pages_table Table name
34
	* @access public
35
	*/
36
	public function __construct(driver_interface $db, $pages_table)
37
	{
38
		$this->db          = $db;
39
		$this->pages_table = $pages_table;
40
	}
41
42
	/**
43
	* Loads routes defined in directory_cats database.
44
	*
45
	* @param string      $resource Resource (not used, but required by parent interface)
46
	* @param string|null $type     The resource type
47
	*
48
	* @return RouteCollection A RouteCollection instance
49
	*
50
	* @api
51
	*/
52
	public function load($resource, $type = null)
53
	{
54
		$collection = new RouteCollection();
55
56
		$defaults = array(
57
			'_controller'	=> 'ernadoo.phpbbdirectory.controller.categories:view_route',
58
			'page'			=> 1,
59
			'sort_days'		=> 0,
60
			'sort_key'		=> 0,
61
			'sort_dir'		=> 0
62
		);
63
64
		$requirements = array(
65
			'cat_id'	=> '\d+',
66
			'page'		=> '\d+',
67
			'sort_days'	=> '\d+',
68
			'sort_key'	=> 'a|t|r|s|v|p',
69
			'sort_dir'	=> 'a|d',
70
		);
71
72
		$sql = 'SELECT cat_id, cat_route
73
			FROM ' . $this->pages_table;
74
		$result = $this->db->sql_query($sql);
75
		while ($row = $this->db->sql_fetchrow($result))
76
		{
77
			$defaults['cat_id'] = $row['cat_id'];
78
79
			$route = new Route('directory/' . $row['cat_route'] . '/{page}/{sort_days}/{sort_key}/{sort_dir}');
80
			$route->setDefaults($defaults)->setRequirements($requirements);
81
			$collection->add('ernadoo_phpbbdirectory_dynamic_route_' . $row['cat_id'], $route);
82
		}
83
		$this->db->sql_freeresult();
84
85
		return $collection;
86
	}
87
88
	/**
89
	* {@inheritdoc}
90
	*
91
	* @api
92
	*/
93
	public function supports($resource, $type = null)
94
	{
95
		return $type === 'ernadoo_phpbbdirectory_route';
96
	}
97
}
98