Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
78.05% |
32 / 41 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Invoice_Field_Contains | |
78.05% |
32 / 41 |
|
28.57% |
2 / 7 |
13.52 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
73.68% |
14 / 19 |
|
0.00% |
0 / 1 |
6.66 | |||
| get_title | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_slug | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_description | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_data_type | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_category | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jetpack CRM Automation Invoice_Field_Contains condition. |
| 4 | * |
| 5 | * @package automattic/jetpack-crm |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\CRM\Automation\Conditions; |
| 9 | |
| 10 | use Automattic\Jetpack\CRM\Automation\Attribute_Definition; |
| 11 | use Automattic\Jetpack\CRM\Automation\Automation_Exception; |
| 12 | use Automattic\Jetpack\CRM\Automation\Base_Condition; |
| 13 | use Automattic\Jetpack\CRM\Automation\Data_Types\Data_Type; |
| 14 | use Automattic\Jetpack\CRM\Automation\Data_Types\Invoice_Data; |
| 15 | use Automattic\Jetpack\CRM\Entities\Invoice; |
| 16 | |
| 17 | /** |
| 18 | * Invoice_Field_Contains condition class. |
| 19 | * |
| 20 | * @since 6.2.0 |
| 21 | */ |
| 22 | class Invoice_Field_Contains extends Base_Condition { |
| 23 | |
| 24 | /** |
| 25 | * Invoice_Field_Contains 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 | // TODO: Fetch automation fields from our DAL. |
| 35 | $invoice_fields = array( |
| 36 | 'id' => __( 'ID', 'zero-bs-crm' ), |
| 37 | 'id_override' => __( 'Reference', 'zero-bs-crm' ), |
| 38 | 'status' => __( 'Status', 'zero-bs-crm' ), |
| 39 | ); |
| 40 | |
| 41 | $this->valid_operators = array( |
| 42 | 'contains' => __( 'Contains', 'zero-bs-crm' ), |
| 43 | 'does_not_contain' => __( 'Does not contain', 'zero-bs-crm' ), |
| 44 | ); |
| 45 | |
| 46 | $this->set_attribute_definitions( |
| 47 | array( |
| 48 | new Attribute_Definition( 'field', __( 'Field', 'zero-bs-crm' ), __( 'Check this field against a specified value.', 'zero-bs-crm' ), Attribute_Definition::SELECT, $invoice_fields ), |
| 49 | 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 ), |
| 50 | new Attribute_Definition( 'value', __( 'Value', 'zero-bs-crm' ), __( 'Value to compare with the field.', 'zero-bs-crm' ), Attribute_Definition::TEXT ), |
| 51 | ) |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Executes the condition. If the condition is met, the value stored in the |
| 57 | * attribute $condition_met is set to true; otherwise, it is set to false. |
| 58 | * |
| 59 | * @since 6.2.0 |
| 60 | * |
| 61 | * @param Data_Type $data Data passed from the trigger. |
| 62 | * @return void |
| 63 | * |
| 64 | * @throws Automation_Exception If an invalid operator is encountered. |
| 65 | */ |
| 66 | protected function execute( Data_Type $data ) { |
| 67 | /** @var Invoice $invoice */ |
| 68 | $invoice = $data->get_data(); |
| 69 | |
| 70 | $field = $this->get_attributes()['field']; |
| 71 | $operator = $this->get_attributes()['operator']; |
| 72 | $value = $this->get_attributes()['value']; |
| 73 | |
| 74 | $this->check_for_valid_operator( $operator ); |
| 75 | $this->logger->log( 'Condition: ' . $field . ' ' . $operator . ' ' . $value . ' => ' . $invoice->{$field} ); |
| 76 | |
| 77 | switch ( $operator ) { |
| 78 | case 'contains': |
| 79 | $this->condition_met = ( str_contains( $invoice->{$field}, $value ) ); |
| 80 | $this->logger->log( 'Condition met?: ' . ( $this->condition_met ? 'true' : 'false' ) ); |
| 81 | |
| 82 | return; |
| 83 | case 'does_not_contain': |
| 84 | $this->condition_met = ( ! str_contains( $invoice->{$field}, $value ) ); |
| 85 | $this->logger->log( 'Condition met?: ' . ( $this->condition_met ? 'true' : 'false' ) ); |
| 86 | |
| 87 | return; |
| 88 | default: |
| 89 | $this->condition_met = false; |
| 90 | throw new Automation_Exception( |
| 91 | /* Translators: %s is the unimplemented operator. */ |
| 92 | sprintf( __( 'Valid but unimplemented operator: %s', 'zero-bs-crm' ), $operator ), |
| 93 | Automation_Exception::CONDITION_OPERATOR_NOT_IMPLEMENTED |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get the title for the invoice field contains condition. |
| 100 | * |
| 101 | * @since 6.2.0 |
| 102 | * |
| 103 | * @return string The title 'Invoice Field Contains'. |
| 104 | */ |
| 105 | public static function get_title(): string { |
| 106 | return __( 'Invoice Field Contains', 'zero-bs-crm' ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get the slug for the invoice field contains condition. |
| 111 | * |
| 112 | * @since 6.2.0 |
| 113 | * |
| 114 | * @return string The slug 'invoice_field_contains'. |
| 115 | */ |
| 116 | public static function get_slug(): string { |
| 117 | return 'jpcrm/condition/invoice_field_contains'; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the description for the invoice field contains condition. |
| 122 | * |
| 123 | * @since 6.2.0 |
| 124 | * |
| 125 | * @return string The description for the condition. |
| 126 | */ |
| 127 | public static function get_description(): string { |
| 128 | return __( 'Checks if an invoice field contains an expected value', 'zero-bs-crm' ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get the data type. |
| 133 | * |
| 134 | * @since 6.2.0 |
| 135 | * |
| 136 | * @return string The type of the step. |
| 137 | */ |
| 138 | public static function get_data_type(): string { |
| 139 | return Invoice_Data::class; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get the category of the invoice field contains condition. |
| 144 | * |
| 145 | * @since 6.2.0 |
| 146 | * |
| 147 | * @return string The category 'invoice'. |
| 148 | */ |
| 149 | public static function get_category(): string { |
| 150 | return __( 'Invoice', 'zero-bs-crm' ); |
| 151 | } |
| 152 | } |