copy_route   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 126
ccs 54
cts 54
cp 1
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A duplicate_blocks() 0 19 2
A duplicate_route() 0 8 1
A execute() 0 38 2
A delete_route() 0 10 2
A copy_custom_block() 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 copy_route extends base_action
13
{
14
	/** @var \blitze\sitemaker\model\mapper\blocks */
15
	protected $block_mapper;
16
17
	/** @var \blitze\sitemaker\model\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('routes');
31 6
		$this->block_mapper = $this->mapper_factory->create('blocks');
32
33
		$route_data = array(
34 6
			'config'	=> array(),
35 6
			'list'		=> 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
		/** @var \blitze\sitemaker\model\entity\route $from_entity */
52 5
		$copied_route = $this->duplicate_route($from_entity, $route, $ext_name, $style_id);
53
54
		/** @var \blitze\sitemaker\model\entity\route $copied_route */
55 5
		$copied_blocks = $this->duplicate_blocks($from_entity->get_blocks(), $copied_route->get_route_id(), $copied_route->get_style());
56
57 5
		$route_data['config'] = $copied_route->to_array();
58 5
		$route_data['list'] = array_map('array_filter', $copied_blocks);
59
60 5
		return $route_data;
61
	}
62
63
	/**
64
	 * @param string $route
65
	 * @param int $style_id
66
	 */
67 5
	protected function delete_route($route, $style_id)
68
	{
69 5
		$entity = $this->route_mapper->load(array(
70 5
			array('route', '=', $route),
71 5
			array('style', '=', $style_id),
72 5
		));
73
74
		if ($entity)
75 5
		{
76 3
			$this->route_mapper->delete($entity);
77 3
		}
78 5
	}
79
80
	/**
81
	 * Copy the route preferences
82
	 *
83
	 * @param \blitze\sitemaker\model\entity\route $from_entity
84
	 * @param string $route
85
	 * @param string $ext_name
86
	 * @param int $style
87
	 * @return \blitze\sitemaker\model\entity_interface
88
	 */
89 5
	protected function duplicate_route(\blitze\sitemaker\model\entity\route $from_entity, $route, $ext_name, $style)
90
	{
91 5
		$copy = clone $from_entity;
92 5
		$copy->set_route($route)
93 5
			->set_ext_name($ext_name)
94 5
			->set_style($style);
95
96 5
		return $this->route_mapper->save($copy);
97
	}
98
99
	/**
100
	 * Copy the blocks
101
	 *
102
	 * @param \blitze\sitemaker\model\collections\blocks $collection
103
	 * @param int $route_id
104
	 * @param int $style_id
105
	 * @return array
106
	 */
107 5
	protected function duplicate_blocks(\blitze\sitemaker\model\collections\blocks $collection, $route_id, $style_id)
108
	{
109 5
		$blocks = array();
110 5
		foreach ($collection as $entity)
111
		{
112 5
			$copy = clone $entity;
113 5
			$copy->set_style($style_id)
114 5
				->set_route_id($route_id);
115
116
			/** @var \blitze\sitemaker\model\entity\block $copied */
117 5
			$copied = $this->block_mapper->save($copy);
118 5
			$position = $copied->get_position();
119
120 5
			$this->copy_custom_block($entity->get_bid(), $copied);
121
122 5
			$blocks[$position][] = $this->render_block($copied);
123 5
		}
124
125 5
		return $blocks;
126
	}
127
128
	/**
129
	 * @param int $from_bid
130
	 * @param \blitze\sitemaker\model\entity\block $entity
131
	 */
132 5
	protected function copy_custom_block($from_bid, \blitze\sitemaker\model\entity\block $entity)
133
	{
134 5
		if ($entity->get_name() === 'blitze.sitemaker.block.custom')
135 5
		{
136 1
			$block = $this->phpbb_container->get('blitze.sitemaker.block.custom');
137 1
			$block->copy($from_bid, $entity->get_bid());
138 1
		}
139 5
	}
140
}
141