add_block   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 54
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 36 2
A get_block_view() 0 6 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 add_block extends base_action
13
{
14
	/**
15
	 * {@inheritdoc}
16
	 * @throws \blitze\sitemaker\exception\invalid_argument
17
	 */
18 4
	public function execute($style_id)
19
	{
20 4
		$name	= $this->request->variable('block', '');
21 4
		$route	= $this->request->variable('route', '');
22
23 4
		if (($block_instance = $this->block_factory->get_block($name)) === null)
24 4
		{
25 1
			throw new \blitze\sitemaker\exception\invalid_argument(array($name, 'SERVICE_NOT_FOUND'));
26
		}
27
28
		$route_data = array(
29 3
			'route' => substr($route, 0, 100),
30 3
			'style'	=> $style_id,
31 3
		);
32
33
		/** @var \blitze\sitemaker\model\entity\route $route_entity */
34 3
		$route_entity = $this->force_get_route($route_data, true);
35
36 3
		$default_settings = $block_instance->get_config(array());
37 3
		$block_settings = $this->blocks->sync_settings($default_settings);
38
39 3
		$block_mapper = $this->mapper_factory->create('blocks');
40
41 3
		$entity = $block_mapper->create_entity(array(
42 3
			'name'			=> $name,
43 3
			'weight'		=> $this->request->variable('weight', 0),
44 3
			'position'		=> $this->request->variable('position', ''),
45 3
			'route_id'		=> (int) $route_entity->get_route_id(),
46 3
			'style'			=> (int) $style_id,
47 3
			'settings'		=> $block_settings,
48 3
			'view'			=> $this->get_block_view($style_id),
49 3
		));
50
51 3
		$entity = $block_mapper->save($entity);
52
53 3
		return $this->render_block($entity);
54
	}
55
56
	/**
57
	 * @param int $style_id
58
	 * @return string
59
	 */
60 3
	protected function get_block_view($style_id)
61
	{
62 3
		$config_text = $this->phpbb_container->get('config_text');
63 3
		$style_prefs = json_decode($config_text->get('sm_layout_prefs'), true);
64
65 3
		return (isset($style_prefs[$style_id])) ? basename($style_prefs[$style_id]['view']) : '';
66
	}
67
}
68