Completed
Push — develop ( de7659...415144 )
by Daniel
09:44
created

save_blocks::_update_route()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 7
nc 4
nop 2
crap 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 \phpbb\language\language */
15
	protected $translator;
16
17
	/** @var \blitze\sitemaker\model\blocks\mapper\blocks */
18
	protected $block_mapper;
19
20
	/** @var \blitze\sitemaker\model\blocks\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('blocks', 'routes');
32 4
		$this->block_mapper = $this->mapper_factory->create('blocks', 'blocks');
33
34 4
		$route_entity = $this->force_get_route(array(
35 4
			'route'	=> $route,
36 4
			'style'	=> $style_id,
37 4
		));
38
39
		/** @type \blitze\sitemaker\model\blocks\entity\route $route_entity */
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\blocks\entity\route $entity
49
	 * @param array $blocks
50
	 */
51 4
	protected function save(\blitze\sitemaker\model\blocks\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\blocks\entity\route $entity
68
	 * @return array
69
	 */
70 4
	protected function get_blocks(\blitze\sitemaker\model\blocks\entity\route $entity)
71
	{
72 4
		$collection = $entity->get_blocks();
73
74 4
		if (!empty($collection))
75 4
		{
76 4
			return $collection->get_entities();
77
		}
78
		else
79
		{
80
			return array();
81
		}
82
	}
83
84
	/**
85
	 * Delete specified blocks
86
	 *
87
	 * @param array $blocks_to_delete
88
	 */
89 4
	protected function delete_blocks(array $blocks_to_delete)
90
	{
91 4
		if (sizeof($blocks_to_delete))
92 4
		{
93 2
			$this->block_mapper->delete(array('bid', '=', array_keys($blocks_to_delete)));
94 2
		}
95 4
	}
96
97
	/**
98
	 * Update block position and weight
99
	 *
100
	 * @param array $blocks_to_update
101
	 * @param array $data
102
	 */
103 4
	protected function update_blocks(array $blocks_to_update, array $data)
104
	{
105 4
		foreach ($blocks_to_update as $entity)
106
		{
107 2
			$row = $data[$entity->get_bid()];
108
109 2
			$entity->set_position($row['position']);
110 2
			$entity->set_weight($row['weight']);
111 2
			$this->block_mapper->save($entity);
112 4
		}
113 4
	}
114
115
	/**
116
	 * Update route if it has blocks or route is customized, otherwise, delete the route
117
	 *
118
	 * @param array                                       $blocks_to_update
119
	 * @param \blitze\sitemaker\model\blocks\entity\route $entity
120
	 */
121 4
	protected function update_route(array $blocks_to_update, \blitze\sitemaker\model\blocks\entity\route $entity)
122
	{
123 4
		$has_blocks = (sizeof($blocks_to_update)) ? true : false;
124
125 4
		if (!$has_blocks && !$this->route_is_customized($entity->to_array()))
126 4
		{
127 1
			$this->route_mapper->delete($entity);
128 1
		}
129
		else
130
		{
131 3
			$entity->set_has_blocks($has_blocks);
132 3
			$this->route_mapper->save($entity);
133
		}
134 4
	}
135
}
136