Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Cron
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
4
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
 schedule_job
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 delete_expired_codes
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Delete expired codes.
4 *
5 * @package brianhenryie/bh-wp-autologin-urls
6 */
7
8namespace BrianHenryIE\WP_Autologin_URLs\WP_Includes;
9
10use BrianHenryIE\WP_Autologin_URLs\API_Interface;
11use Psr\Log\LoggerAwareTrait;
12use Psr\Log\LoggerInterface;
13
14/**
15 * Defines the cron action name.
16 * Schedules the cron job.
17 * Handles the action when called from wp_cron.
18 * Uses API class to delete codes.
19 */
20class Cron {
21    use LoggerAwareTrait;
22
23    const DELETE_EXPIRED_CODES_JOB_NAME = 'bh_wp_autologin_urls_delete_expired_codes';
24
25    /**
26     * An instance of the plugin API which will delete the expired codes.
27     *
28     * @var API_Interface
29     */
30    protected API_Interface $api;
31
32    /**
33     * Constructor.
34     *
35     * @param API_Interface   $api The class that will handle the actual deleting.
36     * @param LoggerInterface $logger A PSR logger.
37     */
38    public function __construct( API_Interface $api, LoggerInterface $logger ) {
39        $this->setLogger( $logger );
40        $this->api = $api;
41    }
42
43    /**
44     * Schedule the delete_expired_codes job, if it is not already scheduled.
45     *
46     * @hooked plugins_loaded
47     */
48    public function schedule_job(): void {
49
50        $hook = self::DELETE_EXPIRED_CODES_JOB_NAME;
51
52        $next_scheduled_event = wp_next_scheduled( $hook );
53        if ( false === $next_scheduled_event ) {
54            wp_schedule_event( time(), 'daily', $hook );
55        }
56    }
57
58    /**
59     * Simple function to invoke from cron.
60     *
61     * @uses \BrianHenryIE\WP_Autologin_URLs\API_Interface::delete_expired_codes()
62     * @hooked bh_wp_autologin_urls_delete_expired_codes
63     * @see self::DELETE_EXPIRED_CODES_JOB_NAME
64     */
65    public function delete_expired_codes(): void {
66
67        $action_name = current_action();
68        $this->logger->debug( 'bh-wp-autologin-urls delete_expired_codes cron jobs started', array( 'action' => $action_name ) );
69
70        $this->api->delete_expired_codes();
71    }
72}