Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.48% covered (warning)
84.48%
49 / 58
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Contact_Field_Changed
84.48% covered (warning)
84.48%
49 / 58
28.57% covered (danger)
28.57%
2 / 7
12.54
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
34 / 34
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
 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_Field_Changed condition.
4 *
5 * @package automattic/jetpack-crm
6 * @since 6.2.0
7 */
8
9namespace Automattic\Jetpack\CRM\Automation\Conditions;
10
11use Automattic\Jetpack\CRM\Automation\Attribute_Definition;
12use Automattic\Jetpack\CRM\Automation\Automation_Exception;
13use Automattic\Jetpack\CRM\Automation\Base_Condition;
14use Automattic\Jetpack\CRM\Automation\Data_Types\Contact_Data;
15use Automattic\Jetpack\CRM\Automation\Data_Types\Data_Type;
16use Automattic\Jetpack\CRM\Entities\Contact;
17
18/**
19 * Contact_Field_Changed condition class.
20 *
21 * @since 6.2.0
22 */
23class Contact_Field_Changed extends Base_Condition {
24
25    /**
26     * Contact_Field_Changed constructor.
27     *
28     * @since 6.2.0
29     *
30     * @param array $step_data The step data.
31     */
32    public function __construct( array $step_data ) {
33        parent::__construct( $step_data );
34
35        $contact_fields = array(
36            'id'               => __( 'ID', 'zero-bs-crm' ),
37            'fname'            => __( 'First Name', 'zero-bs-crm' ),
38            'lname'            => __( 'Last Name', 'zero-bs-crm' ),
39            'status'           => __( 'Status', 'zero-bs-crm' ),
40            'email'            => __( 'Email', 'zero-bs-crm' ),
41            'addr1'            => __( 'Adddress Line 1', 'zero-bs-crm' ),
42            'addr2'            => __( 'Adddress Line 2', 'zero-bs-crm' ),
43            'city'             => __( 'City', 'zero-bs-crm' ),
44            'county'           => __( 'County', 'zero-bs-crm' ),
45            'country'          => __( 'Country', 'zero-bs-crm' ),
46            'postcode'         => __( 'Postcode', 'zero-bs-crm' ),
47            'secaddr_addr1'    => __( 'Seccond Adddress Line 1', 'zero-bs-crm' ),
48            'secaddr_addr2'    => __( 'Seccond Adddress Line 2', 'zero-bs-crm' ),
49            'secaddr_city'     => __( 'Seccond City', 'zero-bs-crm' ),
50            'secaddr_county'   => __( 'Seccond County', 'zero-bs-crm' ),
51            'secaddr_country'  => __( 'Seccond Country', 'zero-bs-crm' ),
52            'secaddr_postcode' => __( 'Seccond Postcode', 'zero-bs-crm' ),
53            'hometel'          => __( 'Home Telephone', 'zero-bs-crm' ),
54            'worktel'          => __( 'Work Telephone', 'zero-bs-crm' ),
55            'mobtel'           => __( 'Mobile Telephone', 'zero-bs-crm' ),
56        );
57
58        $this->valid_operators = array(
59            'is'     => __( 'Is', 'zero-bs-crm' ),
60            'is_not' => __( 'Is not', 'zero-bs-crm' ),
61        );
62
63        $this->set_attribute_definitions(
64            array(
65                new Attribute_Definition( 'field', __( 'Field', 'zero-bs-crm' ), __( 'Check this field against a specified value.', 'zero-bs-crm' ), Attribute_Definition::SELECT, $contact_fields ),
66                new Attribute_Definition( 'operator', __( 'Operator', 'zero-bs-crm' ), __( 'Determines how the field is compared to the specified value.', 'zero-bs-crm' ), Attribute_Definition::SELECT, $this->valid_operators ),
67                new Attribute_Definition( 'value', __( 'Value', 'zero-bs-crm' ), __( 'Value to compare with the field.', 'zero-bs-crm' ), Attribute_Definition::TEXT ),
68            )
69        );
70    }
71
72    /**
73     * Executes the condition. If the condition is met, the value stored in the
74     * attribute $condition_met is set to true; otherwise, it is set to false.
75     *
76     * @since 6.2.0
77     *
78     * @param Data_Type $data Data passed from the trigger.
79     * @return void
80     *
81     * @throws Automation_Exception If an invalid operator is encountered.
82     */
83    protected function execute( Data_Type $data ) {
84
85        /** @var Contact $contact */
86        $contact = $data->get_data();
87
88        $field    = $this->get_attributes()['field'];
89        $operator = $this->get_attributes()['operator'];
90        $value    = $this->get_attributes()['value'];
91
92        $this->check_for_valid_operator( $operator );
93        $this->logger->log( 'Condition: ' . $field . ' ' . $operator . ' ' . $value . ' => ' . $contact->{$field} );
94
95        switch ( $operator ) {
96            case 'is':
97                $this->condition_met = ( $contact->{$field} === $value );
98                $this->logger->log( 'Condition met?: ' . ( $this->condition_met ? 'true' : 'false' ) );
99                return;
100
101            case 'is_not':
102                $this->condition_met = ( $contact->{$field} !== $value );
103                $this->logger->log( 'Condition met?: ' . ( $this->condition_met ? 'true' : 'false' ) );
104                return;
105
106            default:
107                $this->condition_met = false;
108                throw new Automation_Exception(
109                /* Translators: %s is the unimplemented operator. */
110                    sprintf( __( 'Valid but unimplemented operator: %s', 'zero-bs-crm' ), $operator ),
111                    Automation_Exception::CONDITION_OPERATOR_NOT_IMPLEMENTED
112                );
113        }
114    }
115
116    /**
117     * Get the title for the contact field changed condition.
118     *
119     * @since 6.2.0
120     *
121     * @return string The title 'Contact Field Changed'.
122     */
123    public static function get_title(): ?string {
124        return __( 'Contact Field Changed', 'zero-bs-crm' );
125    }
126
127    /**
128     * Get the slug for the contact field changed condition.
129     *
130     * @since 6.2.0
131     *
132     * @return string The slug 'contact_field_changed'.
133     */
134    public static function get_slug(): string {
135        return 'jpcrm/condition/contact_field_changed';
136    }
137
138    /**
139     * Get the description for the contact field changed condition.
140     *
141     * @since 6.2.0
142     *
143     * @return string The description for the condition.
144     */
145    public static function get_description(): string {
146        return __( 'Checks if a contact field change matches an expected value', 'zero-bs-crm' );
147    }
148
149    /**
150     * Get the category of the contact field changed condition.
151     *
152     * @since 6.2.0
153     *
154     * @return string The category 'jpcrm/contact_condition'.
155     */
156    public static function get_category(): string {
157        return __( 'Contact', 'zero-bs-crm' );
158    }
159
160    /**
161     * Get the data type.
162     *
163     * @since 6.2.0
164     *
165     * @return string The type of the step.
166     */
167    public static function get_data_type(): string {
168        return Contact_Data::class;
169    }
170}