Completed
Push — master ( 1deced...926e5e )
by Daniel
08:35
created

blocks_cleanup   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 20
c 4
b 0
f 1
lcom 1
cbo 2
dl 0
loc 192
ccs 86
cts 86
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A run() 0 9 1
A should_run() 0 4 1
A clean_styles() 0 21 3
A clean_routes() 0 15 4
A clean_blocks() 0 18 4
B clean_custom_blocks() 0 29 3
A get_style_ids() 0 15 2
A is_runnable() 0 4 1
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\cron;
11
12
class blocks_cleanup extends \phpbb\cron\task\base
13
{
14
	/** @var \phpbb\config\config */
15
	protected $config;
16
17
	/** @var \phpbb\db\driver\driver_interface */
18
	protected $db;
19
20
	/** @var \blitze\sitemaker\services\blocks\manager */
21
	protected $manager;
22
23
	/** @var \blitze\sitemaker\services\url_checker */
24
	protected $url_checker;
25
26
	/** @var string */
27
	protected $blocks_table;
28
29
	/** @var string */
30
	protected $cblocks_table;
31
32
	/**
33
	 * Constructor
34
	 *
35
	 * @param \phpbb\config\config						$config					Config object
36
	 * @param \phpbb\db\driver\driver_interface			$db						Database object
37
	 * @param \blitze\sitemaker\services\blocks\manager	$manager				Blocks manager object
38
	 * @param \blitze\sitemaker\services\url_checker	$url_checker			Url checker object
39
	 * @param string									$blocks_table			Name of blocks database table
40
	 * @param string									$cblocks_table			Name of custom blocks database table
41
	 */
42 2
	public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \blitze\sitemaker\services\blocks\manager $manager, \blitze\sitemaker\services\url_checker $url_checker, $blocks_table, $cblocks_table)
43
	{
44 2
		$this->config = $config;
45 2
		$this->db = $db;
46 2
		$this->manager = $manager;
47 2
		$this->url_checker = $url_checker;
48 2
		$this->blocks_table = $blocks_table;
49 2
		$this->cblocks_table = $cblocks_table;
50 2
	}
51
52
	/**
53
	 * Runs this cron task.
54
	 *
55
	 * @return null
56
	 */
57 1
	public function run()
58
	{
59 1
		$this->config->set('sitemaker_blocks_cleanup_last_gc', time());
60
61 1
		$routes = $this->clean_styles();
62 1
		$this->clean_routes($routes);
63 1
		$this->clean_blocks();
64 1
		$this->clean_custom_blocks();
65 1
	}
66
67
	/**
68
	 * Returns whether this cron task can run, given current board configuration.
69
	 *
70
	 * @return bool
71
	 */
72 1
	public function is_runnable()
73 1
	{
74 1
		return true;
75
	}
76
77
	/**
78
	 * Returns whether this cron task should run now, because enough time
79
	 * has passed since it was last run.
80
	 *
81
	 * @return bool
82
	 */
83 1
	public function should_run()
84
	{
85 1
		return $this->config['sitemaker_blocks_cleanup_last_gc'] < time() - $this->config['sitemaker_blocks_cleanup_gc'];
86
	}
87
88
	/**
89
	 * Removes all block routes and blocks belonging to these routes
90
	 * for styles that no longer exist
91
	 */
92 1
	private function clean_styles()
93
	{
94 1
		$routes_ary	= $this->manager->get_routes_per_style();
95 1
		$style_ids	= $this->get_style_ids();
96
97 1
		$routes = array();
98 1
		foreach ($routes_ary as $style_id => $style_routes)
99
		{
100
			// Style no longer exists => remove all routes and blocks for style
101 1
			if (!isset($style_ids[$style_id]))
102 1
			{
103 1
				$this->manager->delete_blocks_by_style($style_id);
104
105 1
				continue;
106
			}
107
108 1
			$routes += $style_routes;
109 1
		}
110
111 1
		return $routes;
112
	}
113
114
	/**
115
	 * Removes all blocks for routes that no longer exist
116
	 */
117 1
	private function clean_routes($routes)
118
	{
119 1
		$board_url = generate_board_url();
120
121 1
		foreach ($routes as $route => $row)
122
		{
123 1
			$url = $board_url . '/' . (($row['ext_name']) ? 'app.php' : '') . $row['route'];
124
125
			// Route no longer exists => remove all blocks for route
126 1
			if ($this->url_checker->exists($url) !== true)
127 1
			{
128 1
				$this->manager->delete_blocks_by_route($route);
129 1
			}
130 1
		}
131 1
	}
132
133
	/**
134
	 * Removes all blocks that (the service) no longer exist
135
	 */
136 1
	private function clean_blocks()
137 1
	{
138 1
		$block_names = $this->manager->get_unique_block_names();
139
140 1
		$blocks = array();
141 1
		foreach ($block_names as $block_name)
142
		{
143 1
			if (!$this->manager->block_exists($block_name))
144 1
			{
145 1
				$blocks[] = $block_name;
146 1
			}
147 1
		}
148
149 1
		if (sizeof($blocks))
150 1
		{
151 1
			$this->manager->delete_blocks_by_name($blocks);
152 1
		}
153 1
	}
154
155
	/**
156
	 * Removes from custom blocks table, any custom blocks no longer present in blocks table
157
	 */
158 1
	private function clean_custom_blocks()
159
	{
160 1
		$sql = $this->db->sql_build_query('SELECT', array(
161 1
			'SELECT'	=> 'cb.block_id',
162
			'FROM'		=> array(
163 1
				$this->cblocks_table    => 'cb',
164 1
			),
165
			'LEFT_JOIN'	=> array(
166
				array(
167 1
					'FROM'	=> array($this->blocks_table => 'b'),
168 1
					'ON'	=> "b.bid = cb.block_id",
169
				)
170 1
			),
171
			'WHERE'		=> "b.bid IS NULL"
172 1
		));
173 1
		$result = $this->db->sql_query($sql);
174
175 1
		$block_ids = array();
176 1
		while ($row = $this->db->sql_fetchrow($result))
177
		{
178 1
			$block_ids[] = $row['block_id'];
179 1
		}
180 1
		$this->db->sql_freeresult($result);
181
182 1
		if (sizeof($block_ids))
183 1
		{
184 1
			$this->db->sql_query('DELETE FROM ' . $this->cblocks_table . ' WHERE ' . $this->db->sql_in_set('block_id', $block_ids));
185 1
		}
186 1
	}
187
188 1
	private function get_style_ids()
189
	{
190
		$sql = 'SELECT style_id, style_name
191 1
			FROM ' . STYLES_TABLE;
192 1
		$result = $this->db->sql_query($sql);
193
194 1
		$style_ids = array();
195 1
		while ($row = $this->db->sql_fetchrow($result))
196
		{
197 1
			$style_ids[$row['style_id']] = $row['style_name'];
198 1
		}
199 1
		$this->db->sql_freeresult($result);
200
201 1
		return $style_ids;
202
	}
203
}
204