Passed
Branch develop (786e4f)
by Daniel
07:32
created

save_block::set_hidden_fields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.7085

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 3
dl 0
loc 10
ccs 4
cts 7
cp 0.5714
crap 3.7085
rs 9.4285
c 0
b 0
f 0
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 save_block extends base_action
13
{
14
	/** @var \blitze\sitemaker\model\mapper\blocks */
15
	protected $block_mapper;
16
17
	/**
18
	 * {@inheritdoc}
19
	 * @throws \blitze\sitemaker\exception\out_of_bounds
20
	 */
21 4
	public function execute($style_id)
22
	{
23 4
		$block_id = $this->request->variable('id', 0);
24 4
		$update_similar = $this->request->variable('similar', false);
25
26 4
		$this->block_mapper = $this->mapper_factory->create('blocks');
27
28
		/** @type \blitze\sitemaker\model\entity\block $entity */
29 4
		if (($entity = $this->block_mapper->load(array('bid', '=', $block_id))) === null)
30 4
		{
31 1
			throw new \blitze\sitemaker\exception\out_of_bounds('bid');
32
		}
33
34 3
		$old_hash = $entity->get_hash();
35
36 3
		$entity->set_permission($this->request->variable('permission', array(0)))
37 3
			->set_class($this->request->variable('class', ''))
38 3
			->set_hide_title($this->request->variable('hide_title', 0))
39 3
			->set_status($this->request->variable('status', 0))
40 3
			->set_type($this->request->variable('type', 0))
41 3
			->set_view($this->request->variable('view', ''))
42 3
			->set_settings($this->get_updated_settings($entity->get_name(), $entity->get_settings()));
43 3
		$entity = $this->block_mapper->save($entity);
44
45 3
		$new_hash = $entity->get_hash();
0 ignored issues
show
Bug introduced by
The method get_hash() does not exist on blitze\sitemaker\model\entity_interface. Since it exists in all sub-types, consider adding an abstract or default implementation to blitze\sitemaker\model\entity_interface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
		/** @scrutinizer ignore-call */ 
46
  $new_hash = $entity->get_hash();
Loading history...
46 3
		$updated_blocks = array();
47 3
		$updated_blocks[$entity->get_bid()] = $this->render_block($entity);
0 ignored issues
show
Bug introduced by
The method get_bid() does not exist on blitze\sitemaker\model\entity_interface. Since it exists in all sub-types, consider adding an abstract or default implementation to blitze\sitemaker\model\entity_interface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
		$updated_blocks[$entity->/** @scrutinizer ignore-call */ get_bid()] = $this->render_block($entity);
Loading history...
48
49 3
		if ($update_similar && $new_hash !== $old_hash)
50 3
		{
51 1
			$updated_blocks += $this->update_similar($old_hash, $new_hash, $entity->get_settings());
0 ignored issues
show
Bug introduced by
The method get_settings() does not exist on blitze\sitemaker\model\entity_interface. Since it exists in all sub-types, consider adding an abstract or default implementation to blitze\sitemaker\model\entity_interface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
			$updated_blocks += $this->update_similar($old_hash, $new_hash, $entity->/** @scrutinizer ignore-call */ get_settings());
Loading history...
52 1
		}
53
54
		return array(
55 3
			'list'	=> $updated_blocks,
56 3
		);
57
	}
58
59
	/**
60
	 * @param string $block_service
61
	 * @param array $db_settings
62
	 * @return array
63
	 */
64 3
	private function get_updated_settings($block_service, array $db_settings)
65
	{
66 3
		$block_instance = $this->block_factory->get_block($block_service);
67 3
		$default_settings = $block_instance->get_config(array());
68
69 3
		$cfg_handler = $this->phpbb_container->get('blitze.sitemaker.blocks.cfg_handler');
70 3
		$submitted_settings = $cfg_handler->get_submitted_settings($default_settings);
71
72 3
		$this->set_hidden_fields($submitted_settings, $default_settings, $db_settings);
73
74 3
		return $submitted_settings;
75
	}
76
77
	/**
78
	 * @param array $submitted_settings
79
	 * @param array $default_settings
80
	 * @param array $db_settings
81
	 * @return void
82
	 */
83 3
	private function set_hidden_fields(array &$submitted_settings, array $default_settings, array $db_settings)
84
	{
85 3
		$not_submitted = array_diff_key($default_settings, $submitted_settings);
86 3
		$hidden_settings = array_intersect_key($db_settings, $not_submitted);
87
88 3
		foreach ($hidden_settings as $field => $value)
89
		{
90
			if ($default_settings[$field]['type'] === 'hidden')
91
			{
92
				$submitted_settings[$field] = $value;
93
			}
94 3
		}
95 3
	}
96
97
	/**
98
	 * @param string $old_hash
99
	 * @param string $new_hash
100
	 * @param array  $settings
101
	 * @return array
102
	 */
103 1
	private function update_similar($old_hash, $new_hash, array $settings)
104
	{
105
		// find all similar blocks
106 1
		$blocks_collection = $this->block_mapper->find(array('hash', '=', $old_hash));
107
108 1
		$blocks = array();
109 1
		foreach ($blocks_collection as $entity)
110
		{
111 1
			$entity->set_hash($new_hash);
112 1
			$entity->set_settings($settings);
113 1
			$this->block_mapper->save($entity);
114
115 1
			$blocks[$entity->get_bid()] = $this->render_block($entity);
116 1
		}
117
118 1
		return $blocks;
119
	}
120
}
121