Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
31.25% covered (danger)
31.25%
5 / 16
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
wrapper
31.25% covered (danger)
31.25%
5 / 16
50.00% covered (danger)
50.00%
3 / 6
28.80
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 is_parametrized
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_ready
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get_url
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 get_html_tag
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 __call
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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;
15
16use phpbb\routing\helper;
17
18/**
19* Cron task wrapper class.
20* Enhances cron tasks with convenience methods that work identically for all tasks.
21*/
22class wrapper
23{
24    /**
25     * @var helper
26     */
27    protected $routing_helper;
28
29    /**
30     * @var task
31     */
32    protected $task;
33
34    /**
35     * @var \phpbb\template\template
36     */
37    protected $template;
38
39    /**
40    * Constructor.
41    *
42    * Wraps a task $task, which must implement cron_task interface.
43    *
44    * @param task    $task                The cron task to wrap.
45    * @param helper    $routing_helper        Routing helper for route generation
46    * @param \phpbb\template\template    $template
47    */
48    public function __construct(task $task, helper $routing_helper, $template)
49    {
50        $this->task = $task;
51        $this->routing_helper = $routing_helper;
52        $this->template = $template;
53    }
54
55    /**
56    * Returns whether the wrapped task is parametrised.
57    *
58    * Parametrized tasks accept parameters during initialization and must
59    * normally be scheduled with parameters.
60    *
61    * @return bool        Whether or not this task is parametrized.
62    */
63    public function is_parametrized()
64    {
65        return $this->task instanceof parametrized;
66    }
67
68    /**
69    * Returns whether the wrapped task is ready to run.
70    *
71    * A task is ready to run when it is runnable according to current configuration
72    * and enough time has passed since it was last run.
73    *
74    * @return bool        Whether the wrapped task is ready to run.
75    */
76    public function is_ready()
77    {
78        return $this->task->is_runnable() && $this->task->should_run();
79    }
80
81    /**
82    * Returns a url through which this task may be invoked via web.
83    *
84    * When system cron is not in use, running a cron task is accomplished
85    * by outputting an image with the url returned by this function as
86    * source.
87    *
88    * @return string        URL through which this task may be invoked.
89    */
90    public function get_url()
91    {
92        $params = [];
93        $params['cron_type'] = $this->get_name();
94        if ($this->task instanceof parametrized)
95        {
96            $params = array_merge($params, $this->task->get_parameters());
97        }
98
99        return $this->routing_helper->route('phpbb_cron_run', $params);
100    }
101
102    /**
103     * Returns HTML for an invisible `img` tag that can be displayed on page
104     * load to trigger a request to the relevant cron task endpoint.
105     *
106     * @return string       HTML to render to trigger cron task
107     */
108    public function get_html_tag()
109    {
110        $this->template->set_filenames([
111            'cron_html_tag' => 'cron.html',
112        ]);
113
114        $this->template->assign_var('CRON_TASK_URL', $this->get_url());
115
116        return (string) $this->template->assign_display('cron_html_tag');
117    }
118
119    /**
120    * Forwards all other method calls to the wrapped task implementation.
121    *
122    * @return mixed
123    */
124    public function __call($name, $args)
125    {
126        return call_user_func_array(array($this->task, $name), $args);
127    }
128}