Completed
Push — develop ( 8387b5...1b51bd )
by Daniel
11:34
created

blocks   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 62
c 0
b 0
f 0
ccs 25
cts 25
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A find_sql() 0 6 2
A delete() 0 15 2
A insert() 0 6 1
A move_blocks_down() 0 10 1
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2015 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\model\mapper;
11
12
use blitze\sitemaker\model\base_mapper;
13
14
class blocks extends base_mapper
15
{
16
	/** @var string */
17
	protected $entity_class = 'blitze\sitemaker\model\entity\block';
18
19
	/** @var string */
20
	protected $entity_pkey = 'bid';
21
22
	/**
23
	 * @param array $sql_where
24
	 * @return string
25
	 */
26 57
	protected function find_sql(array $sql_where)
27
	{
28 57
		return 'SELECT * FROM ' . $this->entity_table .
29 57
			((sizeof($sql_where)) ? ' WHERE ' . join(' AND ', $sql_where) : '') . '
30 57
			ORDER BY position, weight ASC';
31
	}
32
33
	/**
34
	 * @param array|\blitze\sitemaker\model\entity\block $condition
35
	 */
36 11
	public function delete($condition)
37
	{
38 11
		parent::delete($condition);
39
40
		// move blocks up for position
41 10
		if ($condition instanceof $this->entity_class)
42 10
		{
43 2
			$this->db->sql_query('UPDATE ' . $this->entity_table . '
44
				SET weight = weight - 1
45 2
				WHERE weight > ' . (int) $condition->get_weight() . '
46 2
					AND style = ' . (int) $condition->get_style() . '
47 2
					AND route_id = ' . (int) $condition->get_route_id() . "
48 2
					AND position = '" . $this->db->sql_escape($condition->get_position()) . "'");
49 2
		}
50 10
	}
51
52
	/**
53
	 * {@inheritdoc}
54
	 */
55 8
	protected function insert(\blitze\sitemaker\model\entity_interface $entity)
56
	{
57 8
		$this->move_blocks_down($entity);
58
59 8
		return parent::insert($entity);
60
	}
61
62
	/**
63
	 * @param \blitze\sitemaker\model\entity_interface $entity
64
	 */
65 8
	protected function move_blocks_down(\blitze\sitemaker\model\entity_interface $entity)
66
	{
67
		/** @type \blitze\sitemaker\model\entity\block $entity */
68 8
		$sql = 'UPDATE ' . $this->entity_table . '
69
			SET weight = weight + 1
70 8
			WHERE weight >= ' . (int) $entity->get_weight() . '
71 8
				AND route_id = ' . (int) $entity->get_route_id() . '
72 8
				AND style = ' . (int) $entity->get_style();
73 8
		$this->db->sql_query($sql);
74 8
	}
75
}
76