Completed
Pull Request — master (#73)
by rxu
01:19
created

mediaembed::is_runnable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 *
4
 * phpBB Media Embed PlugIn extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2020 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\mediaembed\cron;
12
13
/**
14
 * mediaembed cron task.
15
 */
16
class mediaembed extends \phpbb\cron\task\base
17
{
18
	/** @var \phpbb\config\config $config Config object */
19
	protected $config;
20
21
	/** @var \phpbb\cache\driver\driver_interface */
22
	protected $cache;
23
24
	/**
25
	 * Constructor
26
	 *
27
	 * @param \phpbb\config\config                  $config     Config object
28
	 * @param \phpbb\cache\driver\driver_interface  $cache      Cache jbject
29
	 * @access public
30
	 */
31
	public function __construct(\phpbb\config\config $config, \phpbb\cache\driver\driver_interface $cache)
32
	{
33
		$this->config = $config;
34
		$this->cache = $cache;
35
	}
36
37
	/**
38
	 * {@inheritDoc}
39
	 */
40
	public function run()
41
	{
42
		try
43
		{
44
			$this->mediaembed_purge_cache();
45
		}
46
		catch (\RuntimeException $e)
47
		{
48
			return;
49
		}
50
	}
51
52
	/**
53
	 * {@inheritDoc}
54
	 */
55
	public function is_runnable()
56
	{
57
		return (bool) $this->config['media_embed_enable_cache'];
58
	}
59
60
	/**
61
	 * {@inheritDoc}
62
	 */
63
	public function should_run()
64
	{
65
		return $this->config['mediaembed_last_gc'] < strtotime('24 hours ago');
66
	}
67
68
	/**
69
	 * Purge all MediaEmbed cache files
70
	 */
71
	protected function mediaembed_purge_cache()
72
	{
73
		try
74
		{
75
			$iterator = new \DirectoryIterator($this->cache->cache_dir);
76
		}
77
		catch (\Exception $e)
78
		{
79
			return;
80
		}
81
82 View Code Duplication
		foreach ($iterator as $fileInfo)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
		{
84
			if ($fileInfo->isDot() || $fileInfo->isDir())
85
			{
86
				continue;
87
			}
88
89
			$filename = $fileInfo->getFilename();
90
			if (strpos($filename, 'http.') === 0)
91
			{
92
				$this->cache->remove_file($fileInfo->getPathname());
93
			}
94
		}
95
96
		$this->config->set('mediaembed_last_gc', time(), false);
97
	}
98
}
99