Completed
Push — master ( 22d2e9...b480b5 )
by Daniel
08:26
created

copy_route::copy_custom_block()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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 copy_route extends base_action
13
{
14
	/** @var \blitze\sitemaker\model\blocks\mapper\blocks */
15
	protected $block_mapper;
16
17
	/** @var \blitze\sitemaker\model\blocks\mapper\routes */
18
	protected $route_mapper;
19
20
	/**
21
	 * {@inheritdoc}
22
	 */
23 6
	public function execute($style_id)
24
	{
25 6
		$ext_name = $this->request->variable('ext', '');
26 6
		$route = $this->request->variable('route', '');
27 6
		$from_route = $this->request->variable('from_route', '');
28 6
		$from_style = $this->request->variable('from_style', $style_id);
29
30 6
		$this->route_mapper = $this->mapper_factory->create('blocks', 'routes');
31 6
		$this->block_mapper = $this->mapper_factory->create('blocks', 'blocks');
32
33
		$route_data = array(
34 6
			'config'	=> array(),
35 6
			'data'		=> array(),
36 6
		);
37
38
		$condition = array(
39 6
			array('route', '=', $from_route),
40 6
			array('style', '=', $from_style),
41 6
		);
42
43 6
		if (!($from_entity = $this->route_mapper->load($condition)))
44 6
		{
45 1
			return $route_data;
46
		}
47
48
		// delete the current route and all it's blocks
49 5
		$this->delete_route($route, $style_id);
50
51
		/** @type \blitze\sitemaker\model\blocks\entity\route $from_entity */
52
		/** @type \blitze\sitemaker\model\blocks\entity\route $copied_route */
53 5
		$copied_route = $this->duplicate_route($from_entity, $route, $ext_name, $style_id);
0 ignored issues
show
Compatibility introduced by
$from_entity of type object<blitze\sitemaker\model\entity_interface> is not a sub-type of object<blitze\sitemaker\...el\blocks\entity\route>. It seems like you assume a concrete implementation of the interface blitze\sitemaker\model\entity_interface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
54 5
		$copied_blocks = $this->duplicate_blocks($from_entity->get_blocks(), $copied_route->get_route_id(), $copied_route->get_style());
55
56 5
		$route_data['config'] = $copied_route->to_array();
57 5
		$route_data['data'] = array_map('array_filter', $copied_blocks);
58
59 5
		return $route_data;
60
	}
61
62
	/**
63
	 * @param string $route
64
	 * @param int $style_id
65
	 */
66 5
	protected function delete_route($route, $style_id)
67
	{
68 5
		$entity = $this->route_mapper->load(array(
69 5
			array('route', '=', $route),
70 5
			array('style', '=', $style_id),
71 5
		));
72
73
		if ($entity)
74 5
		{
75 3
			$this->route_mapper->delete($entity);
76 3
		}
77 5
	}
78
79
	/**
80
	 * Copy the route preferences
81
	 *
82
	 * @param \blitze\sitemaker\model\blocks\entity\route $from_entity
83
	 * @param string $route
84
	 * @param string $ext_name
85
	 * @param int $style
86
	 * @return \blitze\sitemaker\model\entity_interface
87
	 */
88 5
	protected function duplicate_route(\blitze\sitemaker\model\blocks\entity\route $from_entity, $route, $ext_name, $style)
89
	{
90 5
		$copy = clone $from_entity;
91 5
		$copy->set_route($route)
92 5
			->set_ext_name($ext_name)
93 5
			->set_style($style);
94
95 5
		return $this->route_mapper->save($copy);
96
	}
97
98
	/**
99
	 * Copy the blocks
100
	 *
101
	 * @param \blitze\sitemaker\model\blocks\collections\blocks $collection
102
	 * @param int $route_id
103
	 * @param int $style_id
104
	 * @return array
105
	 */
106 5
	protected function duplicate_blocks(\blitze\sitemaker\model\blocks\collections\blocks $collection, $route_id, $style_id)
107
	{
108 5
		$blocks = array();
109 5
		foreach ($collection as $entity)
110
		{
111 5
			$copy = clone $entity;
112 5
			$copy->set_style($style_id)
113 5
				->set_route_id($route_id);
114
115
			/** @type \blitze\sitemaker\model\blocks\entity\block $copied */
116 5
			$copied = $this->block_mapper->save($copy);
117 5
			$position = $copied->get_position();
118
119 5
			$this->copy_custom_block($entity->get_bid(), $copied);
120
121 5
			$blocks[$position][] = $this->render_block($copied);
122 5
		}
123
124 5
		return $blocks;
125
	}
126
127
	/**
128
	 * @param int $from_bid
129
	 * @param \blitze\sitemaker\model\blocks\entity\block $entity
130
	 */
131 5
	protected function copy_custom_block($from_bid, \blitze\sitemaker\model\blocks\entity\block $entity)
132
	{
133 5
		if ($entity->get_name() === 'blitze.sitemaker.block.custom')
134 5
		{
135 1
			$block = $this->phpbb_container->get('blitze.sitemaker.block.custom');
136 1
			$block->copy($from_bid, $entity->get_bid());
137 1
		}
138 5
	}
139
}
140