Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
64.81% covered (warning)
64.81%
35 / 54
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WP_REST_Help_Center_Ticket_CSAT
64.81% covered (warning)
64.81%
35 / 54
66.67% covered (warning)
66.67%
2 / 3
4.70
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 register_rest_route
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
1 / 1
1
 submit_rating
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * WP_REST_Help_Center_Ticket_CSAT file.
4 *
5 * @package automattic/jetpack-help-center
6 */
7
8namespace Automattic\Jetpack\Help_Center;
9
10/**
11 * Class WP_REST_Help_Center_Ticket_CSAT.
12 */
13class WP_REST_Help_Center_Ticket_CSAT extends WP_REST_Help_Center_Controller {
14    /**
15     * WP_REST_Help_Center_Ticket_CSAT constructor.
16     *
17     * @param Wpcom_Request_Client|null $wpcom_request_client WP.com request client.
18     */
19    public function __construct( ?Wpcom_Request_Client $wpcom_request_client = null ) {
20        parent::__construct( $wpcom_request_client );
21        $this->namespace = 'help-center';
22        $this->rest_base = '/csat';
23    }
24
25    /**
26     * Register available routes.
27     */
28    public function register_rest_route() {
29        register_rest_route(
30            $this->namespace,
31            $this->rest_base,
32            array(
33                'methods'             => \WP_REST_Server::CREATABLE,
34                'callback'            => array( $this, 'submit_rating' ),
35                'permission_callback' => '__return_true',
36                'args'                => array(
37                    'ticket_id' => array(
38                        'required' => true,
39                        'type'     => 'int',
40                    ),
41                    'score'     => array(
42                        'required' => true,
43                        'enum'     => array( 'good', 'bad' ),
44                        'type'     => 'string',
45                    ),
46                    'comment'   => array(
47                        'required' => false,
48                        'type'     => 'string',
49                    ),
50                    'reason_id' => array(
51                        'required' => false,
52                        'type'     => 'string',
53                    ),
54                    'test_mode' => array(
55                        'required' => false,
56                        'type'     => 'boolean',
57                    ),
58                ),
59            )
60        );
61    }
62
63    /**
64     * Post customer satisfaction for a ticket.
65     *
66     * @param \WP_REST_Request $request    The request sent to the API.
67     */
68    public function submit_rating( \WP_REST_Request $request ) {
69        $payload = array(
70            'ticket_id' => $request['ticket_id'],
71            'score'     => $request['score'],
72            'comment'   => $request['comment'],
73            'reason_id' => $request['reason_id'],
74            'test_mode' => $request['test_mode'],
75        );
76
77        $body = $this->wpcom_request_client->request(
78            '/help/csat',
79            '2',
80            array(
81                'method' => 'POST',
82            ),
83            $payload
84        );
85
86        if ( is_wp_error( $body ) ) {
87            return $body;
88        }
89
90        $response = json_decode( wp_remote_retrieve_body( $body ) );
91
92        return rest_ensure_response( $response );
93    }
94}