Completed
Push — master ( 1deced...926e5e )
by Daniel
08:35
created

set_route_prefs::_update_or_remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4286
cc 2
eloc 5
nc 2
nop 2
crap 2
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 \blitze\sitemaker\model\blocks\mapper\routes */
15
	protected $route_mapper;
16
17 5
	public function execute($style_id)
18
	{
19 5
		$this->route_mapper = $this->mapper_factory->create('blocks', 'routes');
20
21
		$route = array(
22 5
			'route'	=> $this->request->variable('route', ''),
23 5
			'style'	=> $style_id,
24 5
		);
25
26
		$route_prefs = array(
27 5
			'hide_blocks'	=> $this->request->variable('hide_blocks', false),
28 5
			'ex_positions'	=> array_filter($this->request->variable('ex_positions', array(0 => ''))),
29 5
		);
30
31
		// user has made choices that differ from defaults
32 5
		if ($this->_route_is_customized($route_prefs))
33 5
		{
34
			// create/update route regardless of whether it has blocks or not
35 2
			$entity = $this->_force_get_route($route);
36 2
			$this->_update_route($entity, $route_prefs);
37 2
		}
38
		// user has made choices that match defaults, and route prefs exist in db
39 3
		else if ($entity = $this->route_mapper->load($route))
40 3
		{
41 2
			$this->_update_or_remove($entity, $route_prefs);
42 2
		}
43
44 5
		return array('message' => $this->user->lang('ROUTE_UPDATED'));
45
	}
46
47 2
	protected function _update_or_remove(\blitze\sitemaker\model\blocks\entity\route $entity, array $route_prefs)
48
	{
49
		// route has blocks, so update it
50 2
		if ($this->_route_has_blocks($entity))
51 2
		{
52 1
			$this->_update_route($entity, $route_prefs);
53 1
		}
54
		// route has no blocks, so remove it from db
55
		else
56
		{
57 1
			$this->route_mapper->delete($entity);
58
		}
59 2
	}
60
61 3
	protected function _update_route(\blitze\sitemaker\model\blocks\entity\route $entity, array $route_prefs)
62
	{
63 3
		$entity->set_hide_blocks($route_prefs['hide_blocks']);
64 3
		$entity->set_ex_positions($route_prefs['ex_positions']);
65
66 3
		$this->route_mapper->save($entity);
67 3
	}
68
69 2
	protected function _route_has_blocks(\blitze\sitemaker\model\blocks\entity\route $entity)
70
	{
71 2
		return ($entity && sizeof($entity->get_blocks())) ? true : false;
72
	}
73
}
74