Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.77% covered (success)
96.77%
30 / 31
87.50% covered (warning)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
manager
96.77% covered (success)
96.77%
30 / 31
87.50% covered (warning)
87.50%
7 / 8
16
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 load_tasks
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 load_tasks_from_container
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 find_one_ready_task
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 find_all_ready_tasks
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 find_task
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 get_tasks
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 wrap_task
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;
15
16use phpbb\cron\task\wrapper;
17use phpbb\routing\helper;
18use Symfony\Component\DependencyInjection\ContainerInterface;
19
20/**
21* Cron manager class.
22*
23* Finds installed cron tasks, stores task objects, provides task selection.
24*/
25class manager
26{
27    /**
28     * @var ContainerInterface
29     */
30    protected $phpbb_container;
31
32    /**
33     * @var helper
34     */
35    protected $routing_helper;
36
37    /**
38    * Set of \phpbb\cron\task\wrapper objects.
39    * Array holding all tasks that have been found.
40    *
41    * @var array
42    */
43    protected $tasks = [];
44
45    /**
46     * Flag indicating if $this->tasks contains tasks registered in the container
47     *
48     * @var bool
49     */
50    protected $is_initialised_from_container = false;
51
52    /**
53     * @var string
54     */
55    protected $phpbb_root_path;
56
57    /**
58     * @var string
59     */
60    protected $php_ext;
61
62    /**
63     * @var \phpbb\template\template
64     */
65    protected $template;
66
67    /**
68    * Constructor. Loads all available tasks.
69    *
70    * @param ContainerInterface $phpbb_container Container
71    * @param helper $routing_helper Routing helper
72    * @param string $phpbb_root_path Relative path to phpBB root
73    * @param string $php_ext PHP file extension
74    * @param \phpbb\template\template $template
75    */
76    public function __construct(ContainerInterface $phpbb_container, helper $routing_helper, $phpbb_root_path, $php_ext, $template)
77    {
78        $this->phpbb_container = $phpbb_container;
79        $this->routing_helper = $routing_helper;
80        $this->phpbb_root_path = $phpbb_root_path;
81        $this->php_ext = $php_ext;
82        $this->template = $template;
83    }
84
85    /**
86    * Loads tasks given by name, wraps them
87    * and puts them into $this->tasks.
88    *
89    * @param array|\ArrayObject $tasks        Array of instances of \phpbb\cron\task\task
90    */
91    public function load_tasks($tasks)
92    {
93        foreach ($tasks as $task)
94        {
95            $this->tasks[] = $this->wrap_task($task);
96        }
97    }
98
99    /**
100    * Loads registered tasks from the container, wraps them
101    * and puts them into $this->tasks.
102    */
103    public function load_tasks_from_container()
104    {
105        if (!$this->is_initialised_from_container)
106        {
107            $this->is_initialised_from_container = true;
108
109            /** @var array|\phpbb\di\service_collection $tasks */
110            $tasks = $this->phpbb_container->get('cron.task_collection');
111
112            $this->load_tasks($tasks);
113        }
114    }
115
116    /**
117    * Finds a task that is ready to run.
118    *
119    * If several tasks are ready, any one of them could be returned.
120    *
121    * If no tasks are ready, null is returned.
122    *
123    * @return wrapper|null
124    */
125    public function find_one_ready_task()
126    {
127        $this->load_tasks_from_container();
128
129        shuffle($this->tasks);
130        foreach ($this->tasks as $task)
131        {
132            if ($task->is_ready())
133            {
134                return $task;
135            }
136        }
137        return null;
138    }
139
140    /**
141    * Finds all tasks that are ready to run.
142    *
143    * @return array        List of tasks which are ready to run (wrapped in \phpbb\cron\task\wrapper).
144    */
145    public function find_all_ready_tasks()
146    {
147        $this->load_tasks_from_container();
148
149        $tasks = [];
150        foreach ($this->tasks as $task)
151        {
152            if ($task->is_ready())
153            {
154                $tasks[] = $task;
155            }
156        }
157        return $tasks;
158    }
159
160    /**
161    * Finds a task by name.
162    *
163    * If there is no task with the specified name, null is returned.
164    *
165    * Web runner uses this method to resolve names to tasks.
166    *
167    * @param string                $name Name of the task to look up.
168    * @return wrapper|null    A wrapped task corresponding to the given name, or null.
169    */
170    public function find_task($name)
171    {
172        $this->load_tasks_from_container();
173
174        foreach ($this->tasks as $task)
175        {
176            if ($task->get_name() == $name)
177            {
178                return $task;
179            }
180        }
181        return null;
182    }
183
184    /**
185    * Find all tasks and return them.
186    *
187    * @return array List of all tasks.
188    */
189    public function get_tasks()
190    {
191        $this->load_tasks_from_container();
192
193        return $this->tasks;
194    }
195
196    /**
197    * Wraps a task inside an instance of \phpbb\cron\task\wrapper.
198    *
199    * @param  \phpbb\cron\task\task             $task The task.
200    * @return wrapper    The wrapped task.
201    */
202    public function wrap_task(\phpbb\cron\task\task $task)
203    {
204        return new wrapper($task, $this->routing_helper, $this->template);
205    }
206}