Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
tidy_search
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 is_runnable
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 should_run
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\cron\task\core;
15
16use phpbb\config\config;
17use phpbb\cron\task\base;
18use phpbb\di\exception\di_exception;
19use phpbb\search\backend\search_backend_interface;
20use phpbb\search\search_backend_factory;
21
22/**
23* Tidy search cron task.
24*
25* Will only run when the currently selected search backend supports tidying.
26*/
27class tidy_search extends base
28{
29    /**
30    * Config object
31    * @var config
32    */
33    protected $config;
34
35    /**
36    * Search backend factory
37    * @var search_backend_factory
38    */
39    protected $search_backend_factory;
40
41    /**
42     * Reference to active search backend to avoid calling the factory multiple times
43     * @var search_backend_interface
44     */
45    protected $active_search;
46
47    /**
48     * Constructor.
49     *
50     * @param config $config The config object
51     * @param search_backend_factory $search_backend_factory
52     */
53    public function __construct(config $config, search_backend_factory $search_backend_factory)
54    {
55        $this->config = $config;
56        $this->search_backend_factory = $search_backend_factory;
57    }
58
59    /**
60    * Runs this cron task.
61    *
62    * @return void
63    */
64    public function run()
65    {
66        if ($this->active_search === null)
67        {
68            $this->active_search = $this->search_backend_factory->get_active();
69        }
70
71        $this->active_search->tidy();
72    }
73
74    /**
75    * Returns whether this cron task can run, given current board configuration.
76    *
77    * Search cron task is runnable in all normal use. It may not be
78    * runnable if the search backend implementation selected in board
79    * configuration does not exist.
80    *
81    * @return bool
82    */
83    public function is_runnable()
84    {
85        try
86        {
87            if ($this->active_search === null)
88            {
89                $this->active_search = $this->search_backend_factory->get_active();
90            }
91        }
92        catch (di_exception $e)
93        {
94            return false;
95        }
96
97        return true;
98    }
99
100    /**
101    * Returns whether this cron task should run now, because enough time
102    * has passed since it was last run.
103    *
104    * The interval between search tidying is specified in board
105    * configuration.
106    *
107    * @return bool
108    */
109    public function should_run()
110    {
111        return $this->config['search_last_gc'] < time() - $this->config['search_gc'];
112    }
113}