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

PrepareReviewData::data()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 10
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers\Api\Version1\Response;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Review;
7
8
class PrepareReviewData
9
{
10
    public array $data;
11
12
    public array $fields;
13
14
    public \WP_REST_Post_Meta_Fields $meta;
15
16
    public \WP_REST_Request $request;
17
18
    public Review $review;
19
20
    /**
21
     * @param string[] $fields
22
     */
23
    public function __construct(array $fields, Review $review, \WP_REST_Request $request)
24
    {
25
        $this->data = [];
26
        $this->fields = $fields;
27
        $this->meta = new \WP_REST_Post_Meta_Fields(glsr()->post_type);
28
        $this->request = $request;
29
        $this->review = $review;
30
    }
31
32
    public function data(): array
33
    {
34
        foreach ($this->fields as $field) {
35
            [$key] = explode('.', $field);
36
            $method = Helper::buildMethodName('prepare', $key);
37
            if (method_exists($this, $method)) {
38
                call_user_func([$this, $method]);
39
            } else {
40
                $this->data[$key] = glsr()->filter("rest-api/reviews/prepare/{$key}", '', $this);
41
            }
42
        }
43
        return $this->data;
44
    }
45
46
    protected function prepareAssignedPosts(): void
47
    {
48
        $this->data['assigned_posts'] = $this->review->assigned_posts;
49
    }
50
51
    protected function prepareAssignedTerms(): void
52
    {
53
        $this->data['assigned_terms'] = $this->review->assigned_terms;
54
    }
55
56
    protected function prepareAssignedUsers(): void
57
    {
58
        $this->data['assigned_users'] = $this->review->assigned_users;
59
    }
60
61
    protected function prepareAuthor(): void
62
    {
63
        $this->data['author'] = $this->review->user_id;
64
    }
65
66
    protected function prepareAvatar(): void
67
    {
68
        $this->data['avatar'] = $this->review->avatar;
69
    }
70
71
    protected function prepareContent(): void
72
    {
73
        $this->data['content'] = $this->review->content;
74
    }
75
76
    protected function prepareCustom(): void
77
    {
78
        $this->data['custom'] = $this->review->custom()->toArray();
79
    }
80
81
    protected function prepareDate(): void
82
    {
83
        $this->data['date'] = mysql_to_rfc3339($this->review->date); // phpcs:ignore
84
    }
85
86
    protected function prepareDateGmt(): void
87
    {
88
        $date = $this->review->date_gmt;
89
        if ('0000-00-00 00:00:00' === $date) {
90
            $date = get_gmt_from_date($this->review->date);
91
        }
92
        $this->data['date_gmt'] = mysql_to_rfc3339($date); // phpcs:ignore
93
    }
94
95
    protected function prepareEmail(): void
96
    {
97
        $this->data['email'] = $this->review->email;
98
    }
99
100
    protected function prepareId(): void
101
    {
102
        $this->data['id'] = $this->review->ID;
103
    }
104
105
    protected function prepareIpAddress(): void
106
    {
107
        $this->data['ip_address'] = $this->review->ip_address;
108
    }
109
110
    protected function prepareIsApproved(): void
111
    {
112
        $this->data['is_approved'] = $this->review->is_approved;
113
    }
114
115
    protected function prepareIsModified(): void
116
    {
117
        $this->data['is_modified'] = $this->review->is_modified;
118
    }
119
120
    protected function prepareIsPinned(): void
121
    {
122
        $this->data['is_pinned'] = $this->review->is_pinned;
123
    }
124
125
    protected function prepareIsVerified(): void
126
    {
127
        $this->data['is_verified'] = $this->review->is_verified;
128
    }
129
130
    protected function prepareMeta(): void
131
    {
132
        $this->data['meta'] = $this->meta->get_value($this->review->ID, $this->request);
133
    }
134
135
    protected function prepareModified(): void
136
    {
137
        $this->data['modified'] = mysql_to_rfc3339($this->review->post()->post_modified); // phpcs:ignore
138
    }
139
140
    protected function prepareModifiedGmt(): void
141
    {
142
        $this->data['modified_gmt'] = mysql_to_rfc3339($this->review->post()->post_modified_gmt); // phpcs:ignore
143
    }
144
145
    protected function prepareName(): void
146
    {
147
        $this->data['name'] = $this->review->author;
148
    }
149
150
    protected function prepareRating(): void
151
    {
152
        $this->data['rating'] = $this->review->rating;
153
    }
154
155
    protected function prepareResponse(): void
156
    {
157
        $this->data['response'] = $this->review->response;
158
    }
159
160
    protected function prepareScore(): void
161
    {
162
        $this->data['score'] = $this->review->score;
163
    }
164
165
    protected function prepareStatus(): void
166
    {
167
        $this->data['status'] = $this->review->status;
168
    }
169
170
    protected function prepareTerms(): void
171
    {
172
        $this->data['terms'] = $this->review->terms;
173
    }
174
175
    protected function prepareTitle(): void
176
    {
177
        $this->data['title'] = $this->review->title;
178
    }
179
180
    protected function prepareType(): void
181
    {
182
        $this->data['type'] = $this->review->type;
183
    }
184
}
185