Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
16 / 20
42.86% covered (danger)
42.86%
3 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Set_Invoice_Status
80.00% covered (warning)
80.00%
16 / 20
42.86% covered (danger)
42.86%
3 / 7
8.51
0.00% covered (danger)
0.00%
0 / 1
 get_slug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_title
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_data_type
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_category
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __construct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 execute
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
1<?php
2/**
3 * Jetpack CRM Automation Set_Invoice_Status action.
4 *
5 * @package automattic/jetpack-crm
6 * @since 6.2.0
7 */
8
9namespace Automattic\Jetpack\CRM\Automation\Actions;
10
11use Automattic\Jetpack\CRM\Automation\Attribute_Definition;
12use Automattic\Jetpack\CRM\Automation\Base_Action;
13use Automattic\Jetpack\CRM\Automation\Data_Types\Data_Type;
14use Automattic\Jetpack\CRM\Automation\Data_Types\Invoice_Data;
15use Automattic\Jetpack\CRM\Entities\Invoice;
16
17/**
18 * Adds the Set_Invoice_Status class.
19 *
20 * @since 6.2.0
21 */
22class Set_Invoice_Status extends Base_Action {
23
24    /**
25     * Get the slug name of the step.
26     *
27     * @since 6.2.0
28     *
29     * @return string The slug name of the step.
30     */
31    public static function get_slug(): string {
32        return 'jpcrm/set_invoice_status';
33    }
34
35    /**
36     * Get the title of the step.
37     *
38     * @since 6.2.0
39     *
40     * @return string|null The title of the step.
41     */
42    public static function get_title(): ?string {
43        return __( 'Set Invoice Status Action', 'zero-bs-crm' );
44    }
45
46    /**
47     * Get the description of the step.
48     *
49     * @since 6.2.0
50     *
51     * @return string|null The description of the step.
52     */
53    public static function get_description(): ?string {
54        return __( 'Action to set the invoice status', 'zero-bs-crm' );
55    }
56
57    /**
58     * Get the data type.
59     *
60     * @since 6.2.0
61     *
62     * @return string The type of the step.
63     */
64    public static function get_data_type(): string {
65        return Invoice_Data::class;
66    }
67
68    /**
69     * Get the category of the step.
70     *
71     * @since 6.2.0
72     *
73     * @return string|null The category of the step.
74     */
75    public static function get_category(): ?string {
76        return __( 'Invoice', 'zero-bs-crm' );
77    }
78
79    /**
80     * Constructor.
81     *
82     * @since 6.2.0
83     *
84     * @param array $step_data The step data.
85     */
86    public function __construct( array $step_data ) {
87        parent::__construct( $step_data );
88
89        // @todo Replace with a select field to improve the user experience and prevent
90        // the user from writing a status that isn't supported.
91        $this->set_attribute_definitions(
92            array(
93                new Attribute_Definition(
94                    'new_status',
95                    __( 'New status', 'zero-bs-crm' ),
96                    __( 'This is the status the invoice should be updated to.', 'zero-bs-crm' ),
97                    Attribute_Definition::TEXT
98                ),
99            )
100        );
101    }
102
103    /**
104     * Update the DAL with the invoice status.
105     *
106     * @since 6.2.0
107     *
108     * @param Data_Type $data Data passed from the trigger.
109     */
110    protected function execute( Data_Type $data ) {
111        if ( empty( $this->get_attribute( 'new_status' ) ) ) {
112            return;
113        }
114
115        /** @var Invoice $invoice */
116        $invoice = $data->get_data();
117
118        global $zbs;
119        $zbs->DAL->invoices->setInvoiceStatus( $invoice->id, $this->get_attribute( 'new_status' ) ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
120    }
121}