Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
37.50% covered (danger)
37.50%
3 / 8
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Boost_Cache_Error
37.50% covered (danger)
37.50%
3 / 8
50.00% covered (danger)
50.00%
2 / 4
18.96
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_error_message
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_error_code
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 to_wp_error
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/*
3 * This file may be called before WordPress is fully initialized. See the README file for info.
4 */
5
6namespace Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress;
7
8/**
9 * A replacement for WP_Error when working in a Pre_WordPress setting.
10 *
11 * This class deliberately offers a similar API to WP_Error for familiarity. All Pre_WordPress functions
12 * which may return an error object use this class to represent an Error state.
13 *
14 * If you call a Pre_WordPress function after loading WordPress, use to_wp_error to convert these
15 * objects to a standard WP_Error object.
16 */
17class Boost_Cache_Error {
18
19    /**
20     * @var string - The error code.
21     */
22    private $code;
23
24    /**
25     * @var string - The error message.
26     */
27    private $message;
28
29    /**
30     * Create a Boost_Cache_Error object, with a code and a message.
31     */
32    public function __construct( $code, $message ) {
33        $this->code    = $code;
34        $this->message = $message;
35    }
36
37    /**
38     * Return the error message.
39     */
40    public function get_error_message() {
41        return $this->message;
42    }
43
44    /**
45     * Return the error code.
46     */
47    public function get_error_code() {
48        return $this->code;
49    }
50
51    /**
52     * Convert to a WP_Error.
53     *
54     * When calling a Pre_WordPress function from a WordPress context, use this method to convert
55     * any resultant errors to WP_Errors for interfacing with other WordPress APIs.
56     *
57     * **Warning** - this function should only be called if WordPress has been loaded!
58     */
59    public function to_wp_error() {
60        if ( ! class_exists( '\WP_Error' ) ) {
61            if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
62                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
63                error_log( 'Warning: Boost_Cache_Error::to_wp_error called from a Pre-WordPress context' );
64            }
65        }
66
67        return new \WP_Error( $this->get_error_code(), $this->get_error_message() );
68    }
69}