Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.97% covered (warning)
70.97%
22 / 31
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Contact_Transitional_Status
70.97% covered (warning)
70.97%
22 / 31
28.57% covered (danger)
28.57%
2 / 7
13.96
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 execute
68.75% covered (warning)
68.75%
11 / 16
0.00% covered (danger)
0.00%
0 / 1
5.76
 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 Contact_Transitional_Status 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\Contact_Data;
14use Automattic\Jetpack\CRM\Automation\Data_Types\Data_Type;
15use Automattic\Jetpack\CRM\Entities\Contact;
16
17/**
18 * Contact_Transitional_Status condition class.
19 *
20 * @since 6.2.0
21 */
22class Contact_Transitional_Status extends Base_Condition {
23
24    /**
25     * Contact_Transitional_Status constructor.
26     *
27     * @since 6.2.0
28     *
29     * @param array $step_data The step data.
30     */
31    public function __construct( array $step_data ) {
32        parent::__construct( $step_data );
33
34        $this->valid_operators = array(
35            'from_to' => __( 'From (...) To (...)', 'zero-bs-crm' ),
36        );
37
38        $this->set_attribute_definitions(
39            array(
40                new Attribute_Definition( 'previous_status_was', __( 'Previous Status Was', 'zero-bs-crm' ), __( 'Value to compare with the previous status.', 'zero-bs-crm' ), Attribute_Definition::TEXT ),
41                new Attribute_Definition( 'new_status_is', __( 'New Status Is', 'zero-bs-crm' ), __( 'Value to compare with the new 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 encountered.
56     */
57    protected function execute( Data_Type $data ) {
58        /** @var Contact $contact */
59        $contact          = $data->get_data();
60        $previous_contact = $data->get_previous_data();
61
62        $operator   = $this->get_attributes()['operator'];
63        $status_was = $this->get_attributes()['previous_status_was'];
64        $status_is  = $this->get_attributes()['new_status_is'];
65
66        $this->check_for_valid_operator( $operator );
67        $this->logger->log( 'Condition: Contact_Transitional_Status ' . $operator . ' ' . $status_was . ' => ' . $status_is );
68
69        switch ( $operator ) {
70            case 'from_to':
71                $this->condition_met = ( $previous_contact->status === $status_was ) && ( $contact->status === $status_is );
72                $this->logger->log( 'Condition met?: ' . ( $this->condition_met ? 'true' : 'false' ) );
73
74                return;
75            default:
76                $this->condition_met = false;
77                throw new Automation_Exception(
78                    /* Translators: %s is the unimplemented operator. */
79                    sprintf( __( 'Valid but unimplemented operator: %s', 'zero-bs-crm' ), $operator ),
80                    Automation_Exception::CONDITION_OPERATOR_NOT_IMPLEMENTED
81                );
82        }
83    }
84
85    /**
86     * Get the title for the contact transitional status condition.
87     *
88     * @since 6.2.0
89     *
90     * @return string The title 'Contact Transitional Status'.
91     */
92    public static function get_title(): string {
93        return __( 'Contact Transitional Status', 'zero-bs-crm' );
94    }
95
96    /**
97     * Get the slug for the contact transitional status condition.
98     *
99     * @since 6.2.0
100     *
101     * @return string The slug 'contact_status_transitional'.
102     */
103    public static function get_slug(): string {
104        return 'jpcrm/condition/contact_status_transitional';
105    }
106
107    /**
108     * Get the description for the contact transitional status condition.
109     *
110     * @since 6.2.0
111     *
112     * @return string The description for the condition.
113     */
114    public static function get_description(): string {
115        return __( 'Checks if a contact status changes from a specified initial value to a designated target one', 'zero-bs-crm' );
116    }
117
118    /**
119     * Get the category of the contact transitional status condition.
120     *
121     * @since 6.2.0
122     *
123     * @return string The category 'contact'.
124     */
125    public static function get_category(): string {
126        return __( 'Contact', 'zero-bs-crm' );
127    }
128
129    /**
130     * Get the data type.
131     *
132     * @since 6.2.0
133     *
134     * @return string The type of the step.
135     */
136    public static function get_data_type(): string {
137        return Contact_Data::class;
138    }
139}