Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.89% covered (warning)
68.89%
31 / 45
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WP_REST_Help_Center_Support_Status
68.89% covered (warning)
68.89%
31 / 45
50.00% covered (danger)
50.00%
2 / 4
7.08
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%
28 / 28
100.00% covered (success)
100.00%
1 / 1
1
 get_support_status
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 get_messaging_support_availability
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * WP_REST_Help_Center_Support_Status file.
4 *
5 * @package automattic/jetpack-help-center
6 */
7
8namespace Automattic\Jetpack\Help_Center;
9
10/**
11 * Class WP_REST_Help_Center_Support_Status.
12 */
13class WP_REST_Help_Center_Support_Status extends WP_REST_Help_Center_Controller {
14    /**
15     * WP_REST_Help_Center_Support_Status 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 = '/support-status';
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::READABLE,
34                'callback'            => array( $this, 'get_support_status' ),
35                'permission_callback' => '__return_true',
36            )
37        );
38
39        register_rest_route(
40            $this->namespace,
41            $this->rest_base . '/messaging',
42            array(
43                'methods'             => \WP_REST_Server::READABLE,
44                'callback'            => array( $this, 'get_messaging_support_availability' ),
45                'permission_callback' => '__return_true',
46                'args'                => array(
47                    'group'       => array(
48                        'type'     => 'string',
49                        'required' => true,
50                    ),
51                    'environment' => array(
52                        'type'     => 'string',
53                        'required' => true,
54                    ),
55                ),
56            )
57        );
58    }
59
60    /**
61     * Should return the support status for the user
62     */
63    public function get_support_status() {
64        $body = $this->wpcom_request_client->request( 'help/support-status' );
65        if ( is_wp_error( $body ) ) {
66            return $body;
67        }
68        $response = json_decode( wp_remote_retrieve_body( $body ) );
69
70        return rest_ensure_response( $response );
71    }
72
73    /**
74     * Should return messaging eligibility
75     *
76     * @param \WP_REST_Request $request    The request sent to the API.
77     */
78    public function get_messaging_support_availability( \WP_REST_Request $request ) {
79        $query_parameters = array(
80            'group'       => $request['group'],
81            'environment' => $request['environment'],
82        );
83        $body             = $this->wpcom_request_client->request( 'help/support-status/messaging?' . http_build_query( $query_parameters ) );
84        if ( is_wp_error( $body ) ) {
85            return $body;
86        }
87        $response = json_decode( wp_remote_retrieve_body( $body ) );
88
89        return rest_ensure_response( $response );
90    }
91}