set_route_prefs   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 92
ccs 32
cts 32
cp 1
rs 10
c 1
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A update_or_remove() 0 11 2
A route_has_blocks() 0 3 3
A update_route() 0 6 1
A execute() 0 37 3
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services\blocks\action;
11
12
class set_route_prefs extends base_action
13
{
14
	/** @var \phpbb\language\language */
15
	protected $translator;
16
17
	/** @var \blitze\sitemaker\model\mapper\routes */
18
	protected $route_mapper;
19
20
	/**
21
	 * {@inheritdoc}
22
	 */
23 5
	public function execute($style_id)
24
	{
25 5
		$this->route_mapper = $this->mapper_factory->create('routes');
26
27
		$ex_positions = $hide_blocks = '';
28 5
		$route_data = array(
29 5
			'route'	=> $this->request->variable('route', ''),
30 5
			'style'	=> $style_id,
31
		);
32
33 5
		$route_prefs = array(
34 5
			'hide_blocks'	=> $this->request->variable('hide_blocks', false),
35 5
			'ex_positions'	=> array_filter($this->request->variable('ex_positions', array(0 => ''))),
36
		);
37
38 5
		// user has made choices that differ from defaults
39 5
		if ($this->route_is_customized($route_prefs))
40
		{
41
			// create/update route regardless of whether it has blocks or not
42 2
			/** @var \blitze\sitemaker\model\entity\route $entity */
43 2
			$entity = $this->force_get_route($route_data);
44 2
			$this->update_route($entity, $route_prefs);
45
46 3
			$ex_positions	= $entity->get_ex_positions();
47 3
			$hide_blocks	= $entity->get_hide_blocks();
48
		}
49 2
		// user has made choices that match defaults, and route prefs exist in db
50 2
		else if ($entity = $this->route_mapper->load($this->get_condition($route_data)))
51
		{
52 5
			/** @var \blitze\sitemaker\model\entity\route $entity */
53
			$this->update_or_remove($entity, $route_prefs);
54
		}
55
56
		return array(
57
			'ex_positions'	=> $ex_positions,
58
			'hide_blocks'	=> $hide_blocks,
59
			'message'		=> $this->translator->lang('ROUTE_UPDATED'),
60
		);
61 2
	}
62
63
	/**
64 2
	 * Updates the route if it has blocks, or removes it if it doesn't
65 2
	 *
66 1
	 * @param \blitze\sitemaker\model\entity\route $entity
67 1
	 * @param array $route_prefs
68
	 */
69
	protected function update_or_remove(\blitze\sitemaker\model\entity\route $entity, array $route_prefs)
70
	{
71 1
		// route has blocks, so update it
72
		if ($this->route_has_blocks($entity))
73 2
		{
74
			$this->update_route($entity, $route_prefs);
75
		}
76
		// route has no blocks, so remove it from db
77
		else
78
		{
79
			$this->route_mapper->delete($entity);
80
		}
81 3
	}
82
83 3
	/**
84 3
	 * Update the route
85
	 *
86 3
	 * @param \blitze\sitemaker\model\entity\route $entity
87 3
	 * @param array $route_prefs
88
	 */
89
	protected function update_route(\blitze\sitemaker\model\entity\route $entity, array $route_prefs)
90
	{
91
		$entity->set_hide_blocks($route_prefs['hide_blocks']);
92
		$entity->set_ex_positions($route_prefs['ex_positions']);
93 2
94
		$this->route_mapper->save($entity);
95 2
	}
96
97
	/**
98
	 * @param \blitze\sitemaker\model\entity\route $entity
99
	 * @return bool
100
	 */
101
	protected function route_has_blocks(\blitze\sitemaker\model\entity\route $entity)
102
	{
103
		return ($entity && sizeof($entity->get_blocks())) ? true : false;
104
	}
105
}
106