Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Cron_Handler
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 wire_callbacks
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 wire_save_post_hook_callbacks
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 wire_cron_hook_callbacks
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Cron Registrar
4 *
5 * @package Choctawnation\CNHSA_Federation
6 */
7
8namespace ChoctawNation\CNHSA_Federation\WP;
9
10use ChoctawNation\CNHSA_Federation\WP\Publisher;
11
12/**
13 * Class Cron_Handler
14 */
15class Cron_Handler {
16    /**
17     * Scheduler instance for handling the scheduling of tasks.
18     *
19     * @var Scheduler $scheduler
20     */
21    private Scheduler $scheduler;
22
23    /**
24     * Service Publisher instance for handling service publishing tasks.
25     *
26     * @var Publisher $publisher
27     */
28    private Publisher $publisher;
29
30    /**
31     * Constructor
32     *
33     * @param Scheduler $scheduler An instance of the Scheduler class to handle scheduling of tasks.
34     * @param Publisher $publisher An instance of the Publisher class to handle publishing tasks.
35     */
36    public function __construct( Scheduler $scheduler, Publisher $publisher ) {
37        $this->scheduler = $scheduler;
38        $this->publisher = $publisher;
39    }
40
41    /**
42     * Wire callbacks to WordPress actions and filters.
43     */
44    public function wire_callbacks() {
45        $this->wire_save_post_hook_callbacks();
46        $this->wire_cron_hook_callbacks();
47    }
48
49    /**
50     * `save_post` handlers -> wire async callbacks for services and locations
51     */
52    private function wire_save_post_hook_callbacks() {
53        $wiring = array(
54            'schedule_services_update'  => 'services',
55            'schedule_locations_update' => 'locations',
56        );
57        foreach ( $wiring as $method => $post_type ) {
58            add_action( "save_post_{$post_type}", array( $this->scheduler, $method ), 10, 3 );
59        }
60    }
61
62    /**
63     * Cron hooks' callbacks (powered by transporters)
64     */
65    private function wire_cron_hook_callbacks() {
66        $post_types = array( 'services', 'locations' );
67        foreach ( $post_types as $post_type ) {
68            $hook   = $this->scheduler->cron_keys[ $post_type ]['update'];
69            $method = 'update_' . $post_type;
70            add_action( $hook, array( $this->publisher, $method ), 10, 2 );
71        }
72    }
73}