Completed
Push — master ( 6a1e13...a0ceb6 )
by Daniel
08:25
created

save_blocks   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 97.92%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 12
c 4
b 2
f 0
lcom 1
cbo 4
dl 0
loc 88
ccs 47
cts 48
cp 0.9792
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 17 1
A _save_blocks() 0 12 1
A _get_blocks() 0 13 2
A _delete_blocks() 0 7 2
A _update_blocks() 0 11 2
A _update_route() 0 14 4
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 save_blocks extends base_action
13
{
14
	/** @var \blitze\sitemaker\model\blocks\mapper\blocks */
15
	protected $block_mapper;
16
17
	/** @var \blitze\sitemaker\model\blocks\mapper\routes */
18
	protected $route_mapper;
19
20 4
	public function execute($style_id)
21
	{
22 4
		$route = $this->request->variable('route', '');
23 4
		$blocks = $this->request->variable('blocks', array(0 => array('' => '')));
24
25 4
		$this->route_mapper = $this->mapper_factory->create('blocks', 'routes');
26 4
		$this->block_mapper = $this->mapper_factory->create('blocks', 'blocks');
27
28 4
		$entity = $this->_force_get_route(array(
29 4
			'route'	=> $route,
30 4
			'style'	=> $style_id,
31 4
		));
32
33 4
		$this->_save_blocks($entity, $blocks);
34
35 4
		return array('message' => $this->user->lang('LAYOUT_SAVED'));
36
	}
37
38 4
	protected function _save_blocks($route_entity, array $blocks)
39
	{
40
		// find all blocks for this route
41 4
		$db_blocks = $this->_get_blocks($route_entity);
42
43 4
		$blocks_to_delete = array_filter(array_diff_key($db_blocks, $blocks));
44 4
		$blocks_to_update = array_filter(array_intersect_key($db_blocks, $blocks));
45
46 4
		$this->_delete_blocks($blocks_to_delete);
47 4
		$this->_update_blocks($blocks_to_update, $blocks);
48 4
		$this->_update_route($blocks_to_update, $route_entity);
49 4
	}
50
51 4
	protected function _get_blocks($entity)
52
	{
53 4
		$collection = $entity->get_blocks();
54
55 4
		if (!empty($collection))
56 4
		{
57 4
			return $collection->get_entities();
58
		}
59
		else
60
		{
61
			return array();
62
		}
63
	}
64
65 4
	protected function _delete_blocks(array $blocks_to_delete)
66
	{
67 4
		if (sizeof($blocks_to_delete))
68 4
		{
69 2
			$this->block_mapper->delete(array('bid', '=', array_keys($blocks_to_delete)));
70 2
		}
71 4
	}
72
73 4
	protected function _update_blocks(array $blocks_to_update, array $data)
74
	{
75 4
		foreach ($blocks_to_update as $entity)
76
		{
77 2
			$row = $data[$entity->get_bid()];
78
79 2
			$entity->set_position($row['position']);
80 2
			$entity->set_weight($row['weight']);
81 2
			$this->block_mapper->save($entity);
82 4
		}
83 4
	}
84
85 4
	protected function _update_route(array $blocks_to_update, $route_entity)
86
	{
87 4
		$has_blocks = (sizeof($blocks_to_update)) ? true : false;
88
89 4
		if (!$has_blocks && !$this->_route_is_customized($route_entity->to_array()))
90 4
		{
91 1
			$this->route_mapper->delete($route_entity);
92 1
		}
93
		else
94
		{
95 3
			$route_entity->set_has_blocks($has_blocks);
96 3
			$this->route_mapper->save($route_entity);
97
		}
98 4
	}
99
}
100