Completed
Push — master ( 4f5aad...a0199d )
by Daniel
09:18
created

save_block::_update_similar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
nop 3
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 save_block extends base_action
13
{
14
	/** @var \blitze\sitemaker\model\blocks\mapper\blocks */
15
	protected $block_mapper;
16
17 4
	public function execute($style_id)
18
	{
19 4
		$block_id = $this->request->variable('id', 0);
20 4
		$update_similar = $this->request->variable('similar', false);
21
22 4
		$this->block_mapper = $this->mapper_factory->create('blocks', 'blocks');
23
24 4
		if (($entity = $this->block_mapper->load(array('bid' => $block_id))) === null)
25 4
		{
26 1
			throw new \blitze\sitemaker\exception\out_of_bounds('BLOCK_NOT_FOUND');
27
		}
28
29 3
		$old_hash = $entity->get_hash();
30
31 3
		$cfg_handler = $this->phpbb_container->get('blitze.sitemaker.blocks.cfg_handler');
32 3
		$block_instance = $this->block_factory->get_block($entity->get_name());
33 3
		$default_settings = $block_instance->get_config(array());
34 3
		$submitted_settings = $cfg_handler->get_submitted_settings($default_settings);
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_no_wrap($this->request->variable('no_wrap', 0))
42 3
			->set_settings($submitted_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, $submitted_settings);
52 1
		}
53
54 3
		return $updated_blocks;
55
	}
56
57 1
	private function _update_similar($old_hash, $new_hash, $settings)
58
	{
59
		// find all similar blocks
60 1
		$blocks_collection = $this->block_mapper->find(array(
61 1
			'hash'	=> $old_hash,
62 1
		));
63
64 1
		$blocks = array();
65 1
		foreach ($blocks_collection as $entity)
66
		{
67 1
			$entity->set_hash($new_hash);
68 1
			$entity->set_settings($settings);
69 1
			$this->block_mapper->save($entity);
70
71 1
			$blocks[$entity->get_bid()] = $this->render_block($entity);
72 1
		}
73
74 1
		return $blocks;
75
	}
76
}
77