Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.22% covered (warning)
72.22%
26 / 36
25.00% covered (danger)
25.00%
2 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Invoice_Status_Changed
72.22% covered (warning)
72.22%
26 / 36
25.00% covered (danger)
25.00%
2 / 8
16.62
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 execute
73.68% covered (warning)
73.68%
14 / 19
0.00% covered (danger)
0.00%
0 / 1
6.66
 is_valid_invoice_status_changed_data
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_title
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_slug
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_description
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_category
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_data_type
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Jetpack CRM Automation Invoice_Status_Changed condition.
4 *
5 * @package automattic/jetpack-crm
6 */
7
8namespace Automattic\Jetpack\CRM\Automation\Conditions;
9
10use Automattic\Jetpack\CRM\Automation\Attribute_Definition;
11use Automattic\Jetpack\CRM\Automation\Automation_Exception;
12use Automattic\Jetpack\CRM\Automation\Base_Condition;
13use Automattic\Jetpack\CRM\Automation\Data_Types\Data_Type;
14use Automattic\Jetpack\CRM\Automation\Data_Types\Invoice_Data;
15
16/**
17 * Invoice_Status_Changed condition class.
18 *
19 * @since 6.2.0
20 */
21class Invoice_Status_Changed extends Base_Condition {
22
23    /**
24     * Invoice_Status_Changed constructor.
25     *
26     * @since 6.2.0
27     *
28     * @param array $step_data The step data.
29     */
30    public function __construct( array $step_data ) {
31        parent::__construct( $step_data );
32
33        $this->valid_operators = array(
34            'is'     => __( 'Is', 'zero-bs-crm' ),
35            'is_not' => __( 'Is not', 'zero-bs-crm' ),
36        );
37
38        $this->set_attribute_definitions(
39            array(
40                new Attribute_Definition( 'operator', __( 'Operator', 'zero-bs-crm' ), __( 'Determines how the status is compared to the specified value.', 'zero-bs-crm' ), Attribute_Definition::SELECT, $this->valid_operators ),
41                new Attribute_Definition( 'value', __( 'Value', 'zero-bs-crm' ), __( 'Value to compare with the status.', 'zero-bs-crm' ), Attribute_Definition::TEXT ),
42            )
43        );
44    }
45
46    /**
47     * Executes the condition. If the condition is met, the value stored in the
48     * attribute $condition_met is set to true; otherwise, it is set to false.
49     *
50     * @since 6.2.0
51     *
52     * @param Data_Type $data Data passed from the trigger.
53     * @return void
54     *
55     * @throws Automation_Exception If an invalid operator is used.
56     */
57    protected function execute( Data_Type $data ) {
58        /** @var Invoice $invoice */
59        $invoice = $data->get_data();
60
61        $field    = 'status';
62        $operator = $this->get_attributes()['operator'];
63        $value    = $this->get_attributes()['value'];
64
65        $this->check_for_valid_operator( $operator );
66        $this->logger->log( 'Condition: ' . $field . ' ' . $operator . ' ' . $value . ' => ' . $invoice->{$field} );
67
68        switch ( $operator ) {
69            case 'is':
70                $this->condition_met = ( $invoice->{$field} === $value );
71                $this->logger->log( 'Condition met?: ' . ( $this->condition_met ? 'true' : 'false' ) );
72
73                return;
74            case 'is_not':
75                $this->condition_met = ( $invoice->{$field} !== $value );
76                $this->logger->log( 'Condition met?: ' . ( $this->condition_met ? 'true' : 'false' ) );
77
78                return;
79            default:
80                $this->condition_met = false;
81                throw new Automation_Exception(
82                /* Translators: %s is the unimplemented operator. */
83                    sprintf( __( 'Valid but unimplemented operator: %s', 'zero-bs-crm' ), $operator ),
84                    Automation_Exception::CONDITION_OPERATOR_NOT_IMPLEMENTED
85                );
86        }
87    }
88
89    /**
90     * Checks if the invoice has at least the necessary keys to detect a status
91     * change.
92     *
93     * @since 6.2.0
94     *
95     * @param array $data The invoice data.
96     * @return bool True if the data is valid to detect a status change, false otherwise
97     */
98    private function is_valid_invoice_status_changed_data( array $data ): bool {
99        return isset( $data['status'] );
100    }
101
102    /**
103     * Get the title for the invoice status changed condition.
104     *
105     * @since 6.2.0
106     *
107     * @return string The title 'Invoice Status Changed'.
108     */
109    public static function get_title(): string {
110        return __( 'Invoice Status Changed', 'zero-bs-crm' );
111    }
112
113    /**
114     * Get the slug for the invoice status changed condition.
115     *
116     * @since 6.2.0
117     *
118     * @return string The slug 'invoice_status_changed'.
119     */
120    public static function get_slug(): string {
121        return 'jpcrm/condition/invoice_status_changed';
122    }
123
124    /**
125     * Get the description for the invoice status changed condition.
126     *
127     * @since 6.2.0
128     *
129     * @return string The description for the condition.
130     */
131    public static function get_description(): string {
132        return __( 'Checks if a invoice status change matches an expected value', 'zero-bs-crm' );
133    }
134
135    /**
136     * Get the category of the invoice status changed condition.
137     *
138     * @since 6.2.0
139     *
140     * @return string The category 'jpcrm/invoice_condition'.
141     */
142    public static function get_category(): string {
143        return __( 'Invoice', 'zero-bs-crm' );
144    }
145
146    /**
147     * Get the data type.
148     *
149     * @since 6.2.0
150     *
151     * @return string The type of the step.
152     */
153    public static function get_data_type(): string {
154        return Invoice_Data::class;
155    }
156}