Passed
Push — develop ( 6f3ffa...e1a061 )
by Paul
14:04
created

RestSummaryController::normalizedArgs()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 16
ccs 0
cts 12
cp 0
rs 9.9332
cc 4
nc 6
nop 1
crap 20
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers\Api\Version1;
4
5
use GeminiLabs\SiteReviews\Controllers\Api\Version1\Parameters\SummaryParameters;
6
use GeminiLabs\SiteReviews\Controllers\Api\Version1\Schema\SummarySchema;
7
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsSummaryShortcode;
8
9
class RestSummaryController extends \WP_REST_Controller
10
{
11
    public function __construct()
12
    {
13
        $obj = get_post_type_object(glsr()->post_type);
14
        $this->namespace = !empty($obj->rest_namespace) ? $obj->rest_namespace : glsr()->id.'/v1';
1 ignored issue
show
Documentation Bug introduced by
It seems like ! empty($obj->rest_names...ce : glsr()->id . '/v1' can also be of type boolean. However, the property $namespace is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
15
        $this->rest_base = 'summary';
16
    }
17
18
    /**
19
     * @return array
20
     */
21
    public function get_collection_params()
22
    {
23
        return glsr(SummaryParameters::class)->parameters();
24
    }
25
26
    /**
27
     * @return array
28
     */
29
    public function get_item_schema()
30
    {
31
        if (empty($this->schema)) {
32
            $this->schema = glsr(SummarySchema::class)->schema();
33
        }
34
        return $this->add_additional_fields_schema($this->schema);
35
    }
36
37
    /**
38
     * @param \WP_REST_Request $request
39
     */
40
    public function get_rating_rendered($request): \WP_REST_Response
41
    {
42
        $args = $this->normalizedArgs($request);
43
        $ratings = glsr_get_ratings($args);
44
        $rendered = glsr_star_rating(
45
            $ratings->average,
46
            $ratings->reviews,
47
            $args
48
        );
49
        return rest_ensure_response([
50
            ...compact('args', 'rendered'),
51
            ...$ratings->toArray(),
52
        ]);
53
    }
54
55
    /**
56
     * @param \WP_REST_Request $request
57
     */
58
    public function get_rating_summary($request): \WP_REST_Response
59
    {
60
        $args = $this->normalizedArgs($request);
61
        if ($request['_rendered']) {
62
            $args['hide'] = $request['_hide'] ?? $request['_rendered_hide'] ?? '';
63
            return rest_ensure_response([
64
                'rendered' => glsr(SiteReviewsSummaryShortcode::class)->build($args, 'rest'),
65
            ]);
66
        }
67
        return rest_ensure_response(glsr_get_ratings($args)->toArray());
68
    }
69
70
    /**
71
     * @param \WP_REST_Request $request
72
     *
73
     * @return true|\WP_Error
74
     */
75
    public function get_summary_permissions_check($request)
76
    {
77
        if (!is_user_logged_in()) {
78
            $message = _x('Sorry, you are not allowed to view rating summaries.', 'admin-text', 'site-reviews');
79
            return new \WP_Error('rest_forbidden_context', $message, [
80
                'status' => rest_authorization_required_code(),
81
            ]);
82
        }
83
        return true;
84
    }
85
86
    /**
87
     * @return void
88
     */
89
    public function register_routes()
90
    {
91
        register_rest_route($this->namespace, "/{$this->rest_base}/rating", [
92
            'schema' => [$this, 'get_public_item_schema'],
93
            [
94
                'args' => $this->get_collection_params(),
95
                'callback' => [$this, 'get_rating_rendered'],
96
                'methods' => \WP_REST_Server::READABLE,
97
                'permission_callback' => [$this, 'get_summary_permissions_check'],
98
            ],
99
        ]);
100
        register_rest_route($this->namespace, "/{$this->rest_base}", [
101
            [
102
                'args' => $this->get_collection_params(),
103
                'callback' => [$this, 'get_rating_summary'],
104
                'methods' => \WP_REST_Server::READABLE,
105
                'permission_callback' => [$this, 'get_summary_permissions_check'],
106
            ],
107
        ]);
108
    }
109
110
    protected function normalizedArgs(\WP_REST_Request $request): array
111
    {
112
        $args = [];
113
        $registered = $this->get_collection_params();
114
        foreach ($registered as $key => $params) {
115
            if (isset($request[$key])) {
116
                $args[$key] = $request[$key];
117
            }
118
        }
119
        if (empty($args['date'])) {
120
            $args['date'] = [
121
                'after' => $args['after'] ?? '',
122
                'before' => $args['before'] ?? '',
123
            ];
124
        }
125
        return glsr()->filterArray("rest-api/{$this->rest_base}/args", $args, $request);
126
    }
127
}
128