blocks   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 27
c 3
b 0
f 0
dl 0
loc 77
ccs 25
cts 25
cp 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A find_sql() 0 4 2
A delete() 0 13 2
A insert() 0 22 2
A move_blocks_down() 0 9 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 59
	protected function find_sql(array $sql_where)
27
	{
28 59
		return 'SELECT * FROM ' . $this->entity_table .
29 59
			((sizeof($sql_where)) ? ' WHERE ' . join(' AND ', $sql_where) : '') . '
30 59
			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 9
	protected function insert(\blitze\sitemaker\model\entity_interface $entity)
56
	{
57 9
		// do we have other existing blocks for route at this position?
58
		/** @type \blitze\sitemaker\model\entity\block $entity */
59 9
		$collection = $this->find(array(
60
			array('route_id', '=', $entity->get_route_id()),
61
			array('style', '=', $entity->get_style()),
62
			array('position', '=', $entity->get_position()),
63
		));
64
65 9
		// if we don't have existing blocks, make sure weight = 0
66
		if (!$collection->valid())
67
		{
68 9
			$entity->set_weight(0);
69
		}
70 9
		// if we have existing blocks, increase block weight by 1 for any block that is below the new block
71 9
		else
72 9
		{
73 9
			$this->move_blocks_down($entity);
74 9
		}
75
76
		return parent::insert($entity);
77
	}
78
79
	/**
80
	 * @param \blitze\sitemaker\model\entity\block $entity
81
	 */
82
	protected function move_blocks_down(\blitze\sitemaker\model\entity\block $entity)
83
	{
84
		$sql = 'UPDATE ' . $this->entity_table . '
85
			SET weight = weight + 1
86
			WHERE weight >= ' . (int) $entity->get_weight() . '
87
				AND route_id = ' . (int) $entity->get_route_id() . '
88
				AND style = ' . (int) $entity->get_style() . "
89
				AND position = '" . $this->db->sql_escape($entity->get_position()) . "'";
90
		$this->db->sql_query($sql);
91
	}
92
}
93