Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
74.29% |
26 / 35 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Quote_Status_Changed | |
74.29% |
26 / 35 |
|
28.57% |
2 / 7 |
17.33 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
73.68% |
14 / 19 |
|
0.00% |
0 / 1 |
9.17 | |||
| 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 Quote_Status_Changed 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\Quote_Data; |
| 15 | use Automattic\Jetpack\CRM\Entities\Quote; |
| 16 | |
| 17 | /** |
| 18 | * Quote_Status_Changed condition class. |
| 19 | * |
| 20 | * @since 6.2.0 |
| 21 | */ |
| 22 | class Quote_Status_Changed extends Base_Condition { |
| 23 | |
| 24 | /** |
| 25 | * Quote_Status_Changed 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 | 'is' => __( 'Is', 'zero-bs-crm' ), |
| 36 | 'is_not' => __( 'Is not', 'zero-bs-crm' ), |
| 37 | ); |
| 38 | |
| 39 | $this->set_attribute_definitions( |
| 40 | array( |
| 41 | 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 ), |
| 42 | new Attribute_Definition( 'value', __( 'Value', 'zero-bs-crm' ), __( 'Value to compare with the status.', 'zero-bs-crm' ), Attribute_Definition::TEXT ), |
| 43 | ) |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Executes the condition. If the condition is met, the value stored in the |
| 49 | * attribute $condition_met is set to true; otherwise, it is set to false. |
| 50 | * |
| 51 | * @since 6.2.0 |
| 52 | * |
| 53 | * @param Data_Type $data The data this condition has to evaluate. |
| 54 | * @return void |
| 55 | * |
| 56 | * @throws Automation_Exception If an invalid operator is encountered. |
| 57 | */ |
| 58 | protected function execute( Data_Type $data ) { |
| 59 | /** @var Quote $quote */ |
| 60 | $quote = $data->get_data(); |
| 61 | |
| 62 | $status_value = ( $quote->accepted > 0 ) ? 'accepted' : ( $quote->template > 0 ? 'published' : 'draft' ); |
| 63 | $operator = $this->get_attributes()['operator']; |
| 64 | $value = $this->get_attributes()['value']; |
| 65 | |
| 66 | $this->check_for_valid_operator( $operator ); |
| 67 | $this->logger->log( 'Condition: quote status ' . $operator . ' ' . $value . ' => ' . $status_value ); |
| 68 | |
| 69 | switch ( $operator ) { |
| 70 | case 'is': |
| 71 | $this->condition_met = ( $status_value === $value ); |
| 72 | $this->logger->log( 'Condition met?: ' . ( $this->condition_met ? 'true' : 'false' ) ); |
| 73 | |
| 74 | return; |
| 75 | case 'is_not': |
| 76 | $this->condition_met = ( $status_value !== $value ); |
| 77 | $this->logger->log( 'Condition met?: ' . ( $this->condition_met ? 'true' : 'false' ) ); |
| 78 | |
| 79 | return; |
| 80 | default: |
| 81 | $this->condition_met = false; |
| 82 | throw new Automation_Exception( |
| 83 | /* Translators: %s is the unimplemented operator. */ |
| 84 | sprintf( __( 'Valid but unimplemented operator: %s', 'zero-bs-crm' ), $operator ), |
| 85 | Automation_Exception::CONDITION_OPERATOR_NOT_IMPLEMENTED |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get the title for the quote status changed condition. |
| 92 | * |
| 93 | * @since 6.2.0 |
| 94 | * |
| 95 | * @return string The title 'Quote Status Changed'. |
| 96 | */ |
| 97 | public static function get_title(): string { |
| 98 | return __( 'Quote Status Changed', 'zero-bs-crm' ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the slug for the quote status changed condition. |
| 103 | * |
| 104 | * @since 6.2.0 |
| 105 | * |
| 106 | * @return string The slug 'quote_status_changed'. |
| 107 | */ |
| 108 | public static function get_slug(): string { |
| 109 | return 'jpcrm/condition/quote_status_changed'; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get the description for the quote status changed condition. |
| 114 | * |
| 115 | * @since 6.2.0 |
| 116 | * |
| 117 | * @return string The description for the condition. |
| 118 | */ |
| 119 | public static function get_description(): string { |
| 120 | return __( 'Checks if a quote status change matches an expected value', 'zero-bs-crm' ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get the data type. |
| 125 | * |
| 126 | * @since 6.2.0 |
| 127 | * |
| 128 | * @return string The type of the step. |
| 129 | */ |
| 130 | public static function get_data_type(): string { |
| 131 | return Quote_Data::class; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get the category of the quote status changed condition. |
| 136 | * |
| 137 | * @since 6.2.0 |
| 138 | * |
| 139 | * @return string The category 'jpcrm/quote_condition'. |
| 140 | */ |
| 141 | public static function get_category(): string { |
| 142 | return __( 'Quote', 'zero-bs-crm' ); |
| 143 | } |
| 144 | } |