Completed
Push — master ( d0d590...4bea8e )
by Daniel
08:46
created

edit_block::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 8
Bugs 0 Features 1
Metric Value
c 8
b 0
f 1
dl 0
loc 30
ccs 18
cts 18
cp 1
rs 8.8571
cc 3
eloc 14
nc 3
nop 1
crap 3
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 edit_block extends base_action
13
{
14
	/**
15
	 * {@inheritdoc}
16
	 * @throws \blitze\sitemaker\exception\out_of_bounds
17
	 */
18 4
	public function execute($style_id)
19
	{
20 4
		$block_id = $this->request->variable('id', 0);
21
22 4
		$cfg_handler = $this->phpbb_container->get('blitze.sitemaker.blocks.cfg_handler');
23 4
		$block_mapper = $this->mapper_factory->create('blocks', 'blocks');
24
25
		/** @type \blitze\sitemaker\model\blocks\entity\block $entity */
26 4
		if (($entity = $block_mapper->load(array('bid', '=', $block_id))) === null)
27 4
		{
28 1
			throw new \blitze\sitemaker\exception\out_of_bounds('bid');
29
		}
30
31 3
		if (($block_instance = $this->block_factory->get_block($entity->get_name())) === null)
32 3
		{
33 1
			throw new \blitze\sitemaker\exception\invalid_argument(array($entity->get_name(), 'SERVICE_NOT_FOUND'));
34
		}
35
36 2
		$default_settings = $block_instance->get_config($entity->get_settings());
37
38 2
		$extension = $this->get_extension($block_instance);
39
40 2
		$this->user->add_lang_ext($extension, 'blocks_admin');
41
42 2
		return array_merge(array(
43 2
				'form' => $cfg_handler->get_edit_form($entity->to_array(), $default_settings),
44 2
			),
45 2
			$this->render_block($entity)
46 2
		);
47
	}
48
49
	/**
50
	 * @param \blitze\sitemaker\services\blocks\driver\block_interface $block_instance
51
	 * @return string
52
	 */
53 2
	protected function get_extension(\blitze\sitemaker\services\blocks\driver\block_interface $block_instance)
54
	{
55 2
		$class_name = get_class($block_instance);
56 2
		list($namespace, $extension) = explode('\\', $class_name);
57
58 2
		return $namespace . '/' . $extension;
59
	}
60
}
61