Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
9 / 11
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
Form_Submission_Error
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
8 / 8
10
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
 get_error_type
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_validation_type
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_system_type
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_system_error
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_validation_error
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 validation_error
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 system_error
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Form_Submission_Error class.
4 *
5 * @package automattic/jetpack-forms
6 */
7
8namespace Automattic\Jetpack\Forms\ContactForm;
9
10use WP_Error;
11
12if ( ! defined( 'ABSPATH' ) ) {
13    exit( 0 );
14}
15
16/**
17 * Custom error class for form submission errors.
18 * Extends WP_Error to add error type classification.
19 */
20class Form_Submission_Error extends WP_Error {
21
22    /**
23     * Error type constants.
24     */
25    const TYPE_VALIDATION = 'validation';
26    const TYPE_SYSTEM     = 'system';
27
28    /**
29     * The error type (validation or system).
30     *
31     * @var string
32     */
33    public $error_type;
34
35    /**
36     * Constructor.
37     *
38     * @param string $code    Error code.
39     * @param string $message Error message.
40     * @param string $type    Error type (validation or system).
41     */
42    public function __construct( $code, $message, $type = self::TYPE_SYSTEM ) {
43        parent::__construct( $code, $message );
44        $this->error_type = $type;
45    }
46
47    /**
48     * Get the error type.
49     *
50     * @return string The error type.
51     */
52    public function get_error_type() {
53        return $this->error_type;
54    }
55
56    /**
57     * Check if this is a validation error.
58     *
59     * @return bool True if validation error, false otherwise.
60     */
61    public function is_validation_type() {
62        return self::TYPE_VALIDATION === $this->error_type;
63    }
64
65    /**
66     * Check if this is a system error.
67     *
68     * @return bool True if system error, false otherwise.
69     */
70    public function is_system_type() {
71        return self::TYPE_SYSTEM === $this->error_type;
72    }
73
74    /**
75     * Check if the given error is a Form Submission Error with system error type.
76     *
77     * @param mixed $error The error to check.
78     * @return bool True if the error is a Form_Submission_Error with system type, false otherwise.
79     */
80    public static function is_system_error( $error ) {
81        return $error instanceof self && $error->is_system_type();
82    }
83
84    /**
85     * Check if the given error is a Form Submission Error with validation error type.
86     *
87     * @param mixed $error The error to check.
88     * @return bool True if the error is a Form_Submission_Error with validation type, false otherwise.
89     */
90    public static function is_validation_error( $error ) {
91        return $error instanceof self && $error->is_validation_type();
92    }
93
94    /**
95     * Create a validation Form Submission Error.
96     *
97     * @param string $code    Error code.
98     * @param string $message Error message.
99     * @return Form_Submission_Error The validation error instance.
100     */
101    public static function validation_error( $code, $message ) {
102        return new self( $code, $message, self::TYPE_VALIDATION );
103    }
104
105    /**
106     * Create a system Form Submission Error.
107     *
108     * @param string $code    Error code.
109     * @param string $message Error message.
110     * @return Form_Submission_Error The system error instance.
111     */
112    public static function system_error( $code, $message ) {
113        return new self( $code, $message, self::TYPE_SYSTEM );
114    }
115}