Completed
Push — develop ( 524863...c767cb )
by Daniel
10:30
created

add_block   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 4
c 3
b 0
f 1
lcom 1
cbo 7
dl 0
loc 55
ccs 28
cts 28
cp 1
rs 10

2 Methods

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