manager::get_all_locations()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 25
rs 9.9
ccs 15
cts 15
cp 1
crap 4
1
<?php
2
/**
3
 *
4
 * Advertisement management. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2017 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ads\location;
12
13
class manager
14
{
15
	/**
16
	 * Array that contains all available template location types which are passed
17
	 * via the service container
18
	 * @var array
19
	 */
20
	protected $template_locations;
21
22
	/**
23
	 * Construct an template locations manager object
24
	 *
25
	 * @param	array	$template_locations	Template location types passed via the service container
26
	 */
27 26
	public function __construct($template_locations)
28
	{
29 26
		$this->register_template_locations($template_locations);
30 26
	}
31
32
	/**
33
	 * Get a list of all template location types
34
	 *
35
	 * If $with_categories is true, returns a composite associated array
36
	 * of location category, ID, name and desc:
37
	 * array(
38
	 *    location_category => array(
39
	 *       location_id => array(
40
	 *          'name' => location_name
41
	 *          'desc' => location_description
42
	 *       ),
43
	 *       ...
44
	 *    ),
45
	 *    ...
46
	 * )
47
	 *
48
	 * Otherwise returns only location ID, name and desc:
49
	 * array(
50
	 *    location_id => array(
51
	 *       'name' => location_name
52
	 *       'desc' => location_description
53
	 *    ),
54
	 *    ...
55
	 * )
56
	 *
57
	 * @param	bool	$with_categories	Should we organize locations into categories?
58
	 *
59
	 * @return	array	Array containing a list of all template locations sorted by categories
60
	 */
61 8
	public function get_all_locations($with_categories = true)
62
	{
63 2
		$location_types = array();
64
65 2
		foreach ($this->template_locations as $location_category_id => $location_category)
66
		{
67 8
			foreach ($location_category as $id => $location_type)
68
			{
69 7
				$body = array(
70 2
					'name'	=> $location_type->get_name(),
71 2
					'desc'	=> $location_type->get_desc(),
72 2
				);
73
74
				if ($with_categories)
75 2
				{
76 1
					$location_types[$location_category_id][$id] = $body;
77 1
				}
78
				else
79
				{
80 1
					$location_types[$id] = $body;
81
				}
82 2
			}
83 2
		}
84
85 2
		return $location_types;
86
	}
87
88
	/**
89
	 * Get a list of all template location IDs for display
90
	 *
91
	 * @return	array	Array containing a list of all template location IDs
92
	 */
93 8
	public function get_all_location_ids()
94
	{
95 8
		$template_locations = array();
96
97 8
		foreach ($this->template_locations as $location_category)
98
		{
99 8
			foreach ($location_category as $location_id => $location)
100
			{
101 8
				if ($location->will_display())
102 8
				{
103 8
					$template_locations[] = $location_id;
104 8
				}
105 8
			}
106 8
		}
107
108 8
		return $template_locations;
109
	}
110
111
	/**
112
	 * Register template locations
113
	 *
114
	 * @param	array	$template_locations	Template location types passed via the service container
115
	 */
116 26
	protected function register_template_locations($template_locations)
117
	{
118 26
		if (!empty($template_locations))
119 26
		{
120
			// Define categories here for custom ordering.
121
			// Static definition also prevents external location
122
			// types to use nondefined category.
123 26
			$this->template_locations = array(
124 26
				'CAT_TOP_OF_PAGE'		=> array(),
125 26
				'CAT_BOTTOM_OF_PAGE'	=> array(),
126 26
				'CAT_IN_POSTS'			=> array(),
127 26
				'CAT_OTHER'				=> array(),
128 26
				'CAT_INTERACTIVE'		=> array(),
129 26
				'CAT_SPECIAL'			=> array(),
130
			);
131
132 26
			foreach ($template_locations as $location)
133
			{
134 26
				$this->template_locations[$location->get_category()][$location->get_id()] = $location;
135 26
			}
136 26
		}
137 26
	}
138
}
139