save_blocks   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 114
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A update_route() 0 12 4
A save() 0 11 1
A delete_blocks() 0 5 2
A update_blocks() 0 9 2
A execute() 0 17 1
A get_blocks() 0 5 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 save_blocks extends base_action
13
{
14
	/** @var \phpbb\language\language */
15
	protected $translator;
16
17
	/** @var \blitze\sitemaker\model\mapper\blocks */
18
	protected $block_mapper;
19
20
	/** @var \blitze\sitemaker\model\mapper\routes */
21
	protected $route_mapper;
22
23
	/**
24
	 * {@inheritdoc}
25
	 */
26 4
	public function execute($style_id)
27
	{
28 4
		$route = $this->request->variable('route', '');
29 4
		$blocks = $this->request->variable('blocks', array(0 => array('' => '')));
30
31 4
		$this->route_mapper = $this->mapper_factory->create('routes');
32 4
		$this->block_mapper = $this->mapper_factory->create('blocks');
33
34 4
		/** @var \blitze\sitemaker\model\entity\route $route_entity */
35 4
		$route_entity = $this->force_get_route(array(
36 4
			'route'	=> $route,
37 4
			'style'	=> $style_id,
38
		));
39
40 4
		$this->save($route_entity, $blocks);
41
42 4
		return array('message' => $this->translator->lang('LAYOUT_SAVED'));
43
	}
44
45
	/**
46
	 * Save blocks for route
47
	 *
48
	 * @param \blitze\sitemaker\model\entity\route $entity
49
	 * @param array $blocks
50
	 */
51 4
	protected function save(\blitze\sitemaker\model\entity\route $entity, array $blocks)
52
	{
53
		// find all blocks for this route
54 4
		$db_blocks = $this->get_blocks($entity);
55
56 4
		$blocks_to_delete = array_filter(array_diff_key($db_blocks, $blocks));
57 4
		$blocks_to_update = array_filter(array_intersect_key($db_blocks, $blocks));
58
59 4
		$this->delete_blocks($blocks_to_delete);
60 4
		$this->update_blocks($blocks_to_update, $blocks);
61 4
		$this->update_route($blocks_to_update, $entity);
62 4
	}
63
64
	/**
65
	 * Get all blocks for route
66
	 *
67
	 * @param \blitze\sitemaker\model\entity\route $entity
68
	 * @return array
69
	 */
70 4
	protected function get_blocks(\blitze\sitemaker\model\entity\route $entity)
71
	{
72 4
		$collection = $entity->get_blocks();
73
74 4
		return (!empty($collection)) ? $collection->get_entities() : array();
75
	}
76
77
	/**
78
	 * Delete specified blocks
79
	 *
80
	 * @param array $blocks_to_delete
81
	 */
82 4
	protected function delete_blocks(array $blocks_to_delete)
83
	{
84 4
		if (sizeof($blocks_to_delete))
85 4
		{
86 2
			$this->block_mapper->delete(array('bid', '=', array_keys($blocks_to_delete)));
87 2
		}
88 4
	}
89
90
	/**
91
	 * Update block position and weight
92
	 *
93
	 * @param array $blocks_to_update
94
	 * @param array $data
95
	 */
96 4
	protected function update_blocks(array $blocks_to_update, array $data)
97
	{
98 4
		foreach ($blocks_to_update as $entity)
99
		{
100 2
			$row = $data[$entity->get_bid()];
101
102 2
			$entity->set_position($row['position']);
103 2
			$entity->set_weight($row['weight']);
104 2
			$this->block_mapper->save($entity);
105 4
		}
106 4
	}
107
108
	/**
109
	 * Update route if it has blocks or route is customized, otherwise, delete the route
110
	 *
111
	 * @param array                                       $blocks_to_update
112
	 * @param \blitze\sitemaker\model\entity\route $entity
113
	 */
114 4
	protected function update_route(array $blocks_to_update, \blitze\sitemaker\model\entity\route $entity)
115
	{
116 4
		$has_blocks = (sizeof($blocks_to_update)) ? true : false;
117
118 4
		if (!$has_blocks && !$this->route_is_customized($entity->to_array()))
119 4
		{
120 1
			$this->route_mapper->delete($entity);
121 1
		}
122
		else
123
		{
124 3
			$entity->set_has_blocks($has_blocks);
125 3
			$this->route_mapper->save($entity);
126
		}
127 4
	}
128
}
129