Test Failed
Push — tmp ( 15f615...89cc97 )
by Paul
10:31 queued 04:40
created

Query::review()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 1
dl 0
loc 19
ccs 0
cts 19
cp 0
crap 12
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Modules\Rating;
8
use GeminiLabs\SiteReviews\Review;
9
10
class Query
11
{
12
    use QuerySql;
13
14
    public $args;
15
    public $db;
16
17
    public function __construct()
18
    {
19
        global $wpdb;
20
        $this->db = $wpdb;
21
    }
22
23
    /**
24
     * @return array
25
     */
26
    public function ratings(array $args)
27
    {
28
        $this->setArgs($args);
29
        $join = implode(' ', $this->sqlClauses([], 'join'));
30
        $and = implode(' ', $this->sqlClauses([], 'and'));
31
        $results = $this->db->get_results("
32
            SELECT r.rating, r.type, COUNT(r.rating) AS count
33
            FROM {$this->table('ratings')} AS r {$join}
34
            WHERE r.is_approved = 1 {$and}
35
            GROUP BY r.type, r.rating
36
        ", ARRAY_A);
37
        return $this->normalizeRatings($results);
38
    }
39
40
    /**
41
     * @todo make sure we delete the cached review when modifying it
42
     * @param int $postId
43
     * @return Review
44
     */
45
    public function review($postId)
46
    {
47
        $reviewId = Helper::castToInt($postId);
48
        if (($review = glsr(Cache::class)->get($reviewId, 'reviews')) instanceof Review) {
49
            return $review;
50
        }
51
        $result = $this->db->get_row("
52
            {$this->sqlSelect()}
53
            {$this->sqlFrom()}
54
            {$this->sqlJoin()}
55
            {$this->sqlJoinPivots()}
56
            WHERE r.review_id = {$reviewId}
57
            GROUP BY r.ID
58
        ");
59
        $review = new Review($result);
60
        if (!empty($result)) {
61
            glsr(Cache::class)->store($reviewId, 'reviews', $review);
62
        }
63
        return $review;
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function reviews(array $args = [], array $postIds = [])
70
    {
71
        $this->setArgs($args);
72
        if (!empty($postIds)) {
73
            $postIds = implode(',', Arr::uniqueInt($postIds));
74
        } else {
75
            $postIds = "SELECT ids.* FROM (
76
                SELECT r.review_id
77
                {$this->sqlFrom()}
78
                {$this->sqlJoinClauses()}
79
                {$this->sqlWhere()}
80
                {$this->sqlOrderBy()}
81
                {$this->sqlLimit()}
82
                {$this->sqlOffset()}
83
            ) as ids";
84
        }
85
        $results = $this->db->get_results("
86
            {$this->sqlSelect()}
87
            {$this->sqlFrom()}
88
            {$this->sqlJoin()}
89
            {$this->sqlJoinPivots()}
90
            WHERE r.review_id in ({$postIds})
91
            GROUP BY r.ID 
92
        ");
93
        foreach ($results as &$result) {
94
            $result = new Review($result);
95
            glsr(Cache::class)->store($result->ID, 'reviews', $result);
96
        }
97
        return $results;
98
    }
99
100
    /**
101
     * @return int
102
     */
103
    public function totalReviews(array $args = [], array $reviews = [])
104
    {
105
        $this->setArgs($args);
106
        if (empty($this->sqlLimit()) && !empty($reviews)) {
107
            return count($reviews);
108
        }
109
        return (int) $this->db->get_var("
110
            SELECT COUNT(*)
111
            {$this->sqlFrom()}
112
            {$this->sqlJoin()}
113
            {$this->sqlWhere()}
114
        ");
115
    }
116
117
    /**
118
     * @param int $postId
119
     * @return bool
120
     */
121
    public function hasRevisions($postId)
122
    {
123
        $revisions = (int) $this->db->get_var("
124
            SELECT COUNT(*) 
125
            FROM {$this->db->posts}
126
            WHERE post_type = 'revision' AND post_parent = {$postId}
127
        ");
128
        return $revisions > 0;
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    protected function normalizeRatings(array $ratings = [])
135
    {
136
        $normalized = [];
137
        foreach ($ratings as $result) {
138
            $type = $result['type'];
139
            if (!array_key_exists($type, $normalized)) {
140
                $normalized[$type] = glsr(Rating::class)->emptyArray();
141
            }
142
            $normalized[$type] = Arr::set($normalized[$type], $result['rating'], $result['count']);
143
        }
144
        return $normalized;
145
    }
146
147
    /**
148
     * @return void
149
     */
150
    public function setArgs(array $args = [])
151
    {
152
        $this->args = (new NormalizeQueryArgs($args))->toArray();
153
    }
154
}
155