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

add_block::get_block_view()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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 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