manager::delete_blocks_by_style()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 1
rs 10
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;
11
12
class manager
13
{
14
	/** @var \phpbb\cache\driver\driver_interface */
15
	protected $cache;
16
17
	/** @var \phpbb\log\log */
18
	protected $log;
19
20
	/** @var \phpbb\user */
21
	protected $user;
22
23
	/** @var \blitze\sitemaker\services\blocks\factory */
24
	protected $block_factory;
25
26
	/** @var \blitze\sitemaker\model\mapper\blocks */
27
	protected $block_mapper;
28
29
	/** @var \blitze\sitemaker\model\mapper\routes */
30
	protected $route_mapper;
31
32
	/**
33 2
	 * Constructor
34
	 *
35 2
	 * @param \phpbb\cache\driver\driver_interface			$cache					Cache driver interface
36 2
	 * @param \phpbb\log\log								$log					The phpBB log system
37
	 * @param \phpbb\user									$user					User object
38 2
	 * @param \blitze\sitemaker\services\blocks\factory		$block_factory			Blocks factory object
39 2
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory			Mapper factory object
40 2
	 */
41
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\log\log $log, \phpbb\user $user, \blitze\sitemaker\services\blocks\factory $block_factory, \blitze\sitemaker\model\mapper_factory $mapper_factory)
42
	{
43
		$this->cache = $cache;
44
		$this->log = $log;
45
		$this->user = $user;
46
		$this->block_factory = $block_factory;
47 1
48
		$this->block_mapper = $mapper_factory->create('blocks');
49 1
		$this->route_mapper = $mapper_factory->create('routes');
50
	}
51 1
52 1
	/**
53
	 * Get all block routes in database
54 1
	 *
55 1
	 * @param string $field
56
	 * @return	array
57 1
	 */
58 1
	public function get_routes($field)
59
	{
60 1
		$collection = $this->route_mapper->find();
61
62
		$routes = array();
63
		foreach ($collection as $entity)
64
		{
65
			$key = $entity->{'get_' . $field}();
66
67
			$routes[$key] = $entity->to_array();
68 1
		}
69
70 1
		return $routes;
71
	}
72 1
73 1
	/**
74
	 * Get unique block names in database
75 1
	 *
76 1
	 * @return array
77
	 */
78 1
	public function get_unique_block_names()
79
	{
80
		$collection = $this->block_mapper->find();
81
82
		$names = array();
83
		foreach ($collection as $entity)
84
		{
85
			$names[] = $entity->get_name();
86
		}
87 1
88
		return array_unique($names);
89 1
	}
90
91
	/**
92
	 * Check if block exists
93
	 *
94
	 * @param	string	$service_name	Service name of block
95
	 * @return	bool
96 1
	 */
97
	public function block_exists($service_name)
98 1
	{
99 1
		return ($this->block_factory->get_block($service_name)) ? true : false;
100
	}
101 1
102 1
	/**
103
	 * Delete all blocks and routes for a specific style
104
	 * @param int|array $style_id
105
	 */
106
	public function delete_blocks_by_style($style_id)
107
	{
108 1
		$this->block_mapper->delete(array('style', '=', $style_id));
109
		$this->route_mapper->delete(array('style', '=', $style_id));
110 1
111
		$this->cache->destroy('sitemaker_block_routes');
112 1
113
		$this->log->add($this->user->data['user_id'], $this->user->ip, null, 'LOG_DELETED_BLOCKS_FOR_STYLE', time(), array($style_id));
114 1
	}
115 1
116 1
	/**
117
	 * Delete a route and all it's blocks across styles
118
	 * @param string|array $route
119
	 */
120
	public function delete_blocks_by_route($route)
121
	{
122 1
		$collection = $this->route_mapper->find(array('route', '=', $route));
123
		$route_ids = array_keys($collection->get_entities());
124 1
125
		$missing_routes = [];
126 1
		foreach ($collection as $entity)
127
		{
128 1
			$missing_routes[] = $entity->get_route();
129 1
		}
130 1
131
		$this->block_mapper->delete(array('route_id', '=', $route_ids));
132
		$this->route_mapper->delete(array('route_id', '=', $route_ids));
133
134
		$this->log->add($this->user->data['user_id'], $this->user->ip, null, 'LOG_DELETED_BLOCKS_FOR_ROUTE', time(), array(join("<br />", $missing_routes)));
135
	}
136
137
	/**
138
	 * Delete all instances of a block across styles/routes
139
	 * @param string|array $block_name
140
	 */
141
	public function delete_blocks_by_name($block_name)
142
	{
143
		$collection = $this->block_mapper->find(array('name', '=', $block_name));
144
145
		foreach ($collection as $entity)
146
		{
147
			$this->block_mapper->delete($entity);
148
		}
149
150
		$this->log->add($this->user->data['user_id'], $this->user->ip, null, 'LOG_DELETED_BLOCKS', time(), array($block_name));
151
	}
152
}
153