Test Failed
Push — develop ( c6b569...7a7654 )
by Paul
09:40
created

RestShortcodeController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
dl 0
loc 52
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_items_permissions_check() 0 14 3
A get_items() 0 5 1
A register_routes() 0 7 1
A __construct() 0 4 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers\Api\Version1;
4
5
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
6
7
class RestShortcodeController extends \WP_REST_Controller
8
{
9
    public function __construct()
10
    {
11
        $this->namespace = glsr()->id.'/v1';
12
        $this->rest_base = 'shortcode';
13
    }
14
15
    /**
16
     * @param \WP_REST_Request $request
17
     *
18
     * @return \WP_REST_Response|\WP_Error
19
     */
20
    public function get_items($request)
21
    {
22
        $shortcode = glsr()->shortcode($request['shortcode']);
0 ignored issues
show
Bug introduced by
It seems like $request['shortcode'] can also be of type null; however, parameter $shortcode of GeminiLabs\SiteReviews\Application::shortcode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
        $shortcode = glsr()->shortcode(/** @scrutinizer ignore-type */ $request['shortcode']);
Loading history...
23
        return rest_ensure_response(
24
            $shortcode->apiFetchResponse($request)
0 ignored issues
show
Bug introduced by
The method apiFetchResponse() does not exist on GeminiLabs\SiteReviews\Contracts\ShortcodeContract. Since it exists in all sub-types, consider adding an abstract or default implementation to GeminiLabs\SiteReviews\Contracts\ShortcodeContract. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
            $shortcode->/** @scrutinizer ignore-call */ 
25
                        apiFetchResponse($request)
Loading history...
25
        );
26
    }
27
28
    /**
29
     * @param \WP_REST_Request $request
30
     *
31
     * @return true|\WP_Error
32
     */
33
    public function get_items_permissions_check($request)
34
    {
35
        if (!is_user_logged_in()) {
36
            $error = _x('Sorry, you are not allowed to do that.', 'admin-text', 'site-reviews');
37
            return new \WP_Error('rest_forbidden_context', $error, [
38
                'status' => rest_authorization_required_code(),
39
            ]);
40
        }
41
        if (!glsr()->shortcode($request['shortcode'])) {
0 ignored issues
show
Bug introduced by
It seems like $request['shortcode'] can also be of type null; however, parameter $shortcode of GeminiLabs\SiteReviews\Application::shortcode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        if (!glsr()->shortcode(/** @scrutinizer ignore-type */ $request['shortcode'])) {
Loading history...
42
            return new \WP_Error('rest_invalid_shortcode', 'Invalid shortcode', [
43
                'status' => 400,
44
            ]);
45
        }
46
        return true;
47
    }
48
49
    /**
50
     * @return void
51
     */
52
    public function register_routes()
53
    {
54
        register_rest_route($this->namespace, "/{$this->rest_base}/(?P<shortcode>[a-z_]+)", [
55
            [
56
                'callback' => [$this, 'get_items'],
57
                'methods' => \WP_REST_Server::READABLE,
58
                'permission_callback' => [$this, 'get_items_permissions_check'],
59
            ],
60
        ]);
61
    }
62
}
63