/var/www/html_uk/wp-content/plugins/automatewoo/includes/Jobs/ToolTaskRunner.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php

namespace AutomateWoo\Jobs;

use 
AutomateWoo\ActionScheduler\ActionSchedulerInterface;
use 
AutomateWoo\Exceptions\InvalidArgument;
use 
AutomateWoo\Tool_Background_Processed_Abstract;
use 
AutomateWoo\Tools\ToolsService;
use 
Exception;
use 
RuntimeException;

defined'ABSPATH' ) || exit;

/**
 * Class ToolTaskRunner
 *
 * Runs background tasks as required by AutomateWoo\Tools.
 *
 * @since 5.2.0.
 */
class ToolTaskRunner extends AbstractOneTimeActionSchedulerJob {

    
/**
     * Get the name of the job.
     *
     * @return string
     */
    
public function get_name() {
        return 
'tools';
    }

    
/**
     * @var ToolsService
     */
    
protected $tools_service;

    
/**
     * ToolTaskRunner constructor.
     *
     * @param ActionSchedulerInterface  $action_scheduler
     * @param ActionSchedulerJobMonitor $monitor
     * @param ToolsService              $tools
     */
    
public function __constructActionSchedulerInterface $action_schedulerActionSchedulerJobMonitor $monitorToolsService $tools ) {
        
$this->tools_service $tools;
        
parent::__construct$action_scheduler$monitor );
    }

    
/**
     * Process a single item.
     *
     * @param array $item A single item to process. Expects a validated item.
     *
     * @throws Exception If an error occurs. The exception will be logged by ActionScheduler.
     * @throws RuntimeException If tool is not found.
     */
    
protected function process_item( array $item ) {
        
$tool $this->tools_service->get_tool$item['tool_id'] );
        if ( ! 
$tool || ! $tool instanceof Tool_Background_Processed_Abstract ) {
            throw new 
RuntimeException'Valid tool not found.' );
        }

        
$tool->handle_background_task$item );
    }

    
/**
     * Validate an item to be processed by the job.
     *
     * @param array $item
     *
     * @throws InvalidArgument If the item is not valid.
     */
    
protected function validate_item( array $item ) {
        
parent::validate_item$item );

        if ( ! isset( 
$item['tool_id'] ) ) {
            throw 
InvalidArgument::missing_required'tool_id' );
        }
    }
}