Completed
Push — develop ( 8abf2d...b28862 )
by Daniel
09:52
created

save_block::execute()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 25
cts 25
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 22
nc 3
nop 1
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_block extends base_action
13
{
14
	/** @var \blitze\sitemaker\model\mapper\blocks */
15
	protected $block_mapper;
16
17
	/**
18
	 * {@inheritdoc}
19
	 * @throws \blitze\sitemaker\exception\out_of_bounds
20
	 */
21 4
	public function execute($style_id)
22
	{
23 4
		$block_id = $this->request->variable('id', 0);
24 4
		$update_similar = $this->request->variable('similar', false);
25
26 4
		$this->block_mapper = $this->mapper_factory->create('blocks');
27
28
		/** @type \blitze\sitemaker\model\entity\block $entity */
29 4
		if (($entity = $this->block_mapper->load(array('bid', '=', $block_id))) === null)
30 4
		{
31 1
			throw new \blitze\sitemaker\exception\out_of_bounds('bid');
32
		}
33
34 3
		$old_hash = $entity->get_hash();
35
36 3
		$entity->set_permission($this->request->variable('permission', array(0)))
37 3
			->set_class($this->request->variable('class', ''))
38 3
			->set_hide_title($this->request->variable('hide_title', 0))
39 3
			->set_status($this->request->variable('status', 0))
40 3
			->set_type($this->request->variable('type', 0))
41 3
			->set_view($this->request->variable('view', ''))
42 3
			->set_settings($this->get_updated_settings($entity->get_name(), $entity->get_settings()));
43 3
		$entity = $this->block_mapper->save($entity);
44
45 3
		$new_hash = $entity->get_hash();
46 3
		$updated_blocks = array();
47 3
		$updated_blocks[$entity->get_bid()] = $this->render_block($entity);
48
49 3
		if ($update_similar && $new_hash !== $old_hash)
50 3
		{
51 1
			$updated_blocks += $this->update_similar($old_hash, $new_hash, $entity->get_settings());
52 1
		}
53
54
		return array(
55 3
			'list'	=> $updated_blocks,
56 3
		);
57
	}
58
59
	/**
60
	 * @param string $block_service
61
	 * @param array $db_settings
62
	 * @return array
63
	 */
64 3
	private function get_updated_settings($block_service, array $db_settings)
65
	{
66 3
		$block_instance = $this->block_factory->get_block($block_service);
67 3
		$default_settings = $block_instance->get_config(array());
68
69 3
		$cfg_handler = $this->phpbb_container->get('blitze.sitemaker.blocks.cfg_handler');
70 3
		$submitted_settings = $cfg_handler->get_submitted_settings($default_settings);
71
72 3
		$not_submitted = array_diff_key($default_settings, $submitted_settings);
73 3
		$hidden_settings = array_intersect_key($db_settings, $not_submitted);
74
75 3
		return array_merge($submitted_settings, $hidden_settings);
76 1
	}
77
78
	/**
79
	 * @param string $old_hash
80
	 * @param string $new_hash
81
	 * @param array  $settings
82
	 * @return array
83
	 */
84 1
	private function update_similar($old_hash, $new_hash, array $settings)
85
	{
86
		// find all similar blocks
87 1
		$blocks_collection = $this->block_mapper->find(array('hash', '=', $old_hash));
88
89 1
		$blocks = array();
90 1
		foreach ($blocks_collection as $entity)
91
		{
92 1
			$entity->set_hash($new_hash);
93 1
			$entity->set_settings($settings);
94 1
			$this->block_mapper->save($entity);
95
96 1
			$blocks[$entity->get_bid()] = $this->render_block($entity);
97 1
		}
98
99 1
		return $blocks;
100
	}
101
}
102