Passed
Push — master ( 6fa460...6bbb33 )
by Paul
18:19 queued 07:35
created

Export   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 31
c 1
b 0
f 1
dl 0
loc 102
ccs 0
cts 77
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A exportWithSlugs() 0 3 1
A sqlAssignedSlugs() 0 28 1
A __construct() 0 9 1
A sqlAssignedIds() 0 27 1
A export() 0 3 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Database;
6
7
class Export
8
{
9
    protected $assignedPostsTable;
10
    protected $assignedTermsTable;
11
    protected $assignedUsersTable;
12
    protected $db;
13
    protected $postType;
14
    protected $ratingsTable;
15
16
    public function __construct()
17
    {
18
        global $wpdb;
19
        $this->assignedPostsTable = glsr(Query::class)->table('assigned_posts');
20
        $this->assignedTermsTable = glsr(Query::class)->table('assigned_terms');
21
        $this->assignedUsersTable = glsr(Query::class)->table('assigned_users');
22
        $this->db = $wpdb;
23
        $this->postType = glsr()->post_type;
24
        $this->ratingsTable = glsr(Query::class)->table('ratings');
25
    }
26
27
    /**
28
     * @return array
29
     */
30
    public function export()
31
    {
32
        return glsr(Database::class)->dbGetResults($this->sqlAssignedIds(), 'ARRAY_A');
33
    }
34
35
    /**
36
     * @return array
37
     */
38
    public function exportWithSlugs()
39
    {
40
        return glsr(Database::class)->dbGetResults($this->sqlAssignedSlugs(), 'ARRAY_A');
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    protected function sqlAssignedIds()
47
    {
48
        return glsr(Query::class)->sql("
49
            SELECT
50
                p.post_date AS date,
51
                p.post_date_gmt AS date_gmt,
52
                p.post_title AS title,
53
                p.post_content AS content,
54
                r.rating,
55
                r.name,
56
                r.email,
57
                r.avatar,
58
                r.ip_address,
59
                r.is_approved,
60
                r.is_pinned,
61
                r.terms,
62
                GROUP_CONCAT(DISTINCT apt.post_id) AS assigned_posts,
63
                GROUP_CONCAT(DISTINCT att.term_id) AS assigned_terms,
64
                GROUP_CONCAT(DISTINCT aut.user_id) AS assigned_users,
65
                GROUP_CONCAT(DISTINCT pm.meta_value) as response
66
            FROM {$this->ratingsTable} AS r
67
            INNER JOIN {$this->db->posts} AS p ON r.review_id = p.ID
68
            LEFT JOIN {$this->assignedPostsTable} AS apt ON r.ID = apt.rating_id
69
            LEFT JOIN {$this->assignedTermsTable} AS att ON r.ID = att.rating_id
70
            LEFT JOIN {$this->assignedUsersTable} AS aut ON r.ID = aut.rating_id
71
            LEFT JOIN gl_postmeta AS pm ON (r.review_id = pm.post_id AND pm.meta_key = '_response')
72
            WHERE p.post_type = '{$this->postType}'
73
            AND p.post_status IN ('publish','pending')
74
            GROUP BY r.ID
75
        ");
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    protected function sqlAssignedSlugs()
82
    {
83
        return glsr(Query::class)->sql("
84
            SELECT
85
                p.post_date AS date,
86
                p.post_date_gmt AS date_gmt,
87
                p.post_title AS title,
88
                p.post_content AS content,
89
                r.rating,
90
                r.name,
91
                r.email,
92
                r.avatar,
93
                r.ip_address,
94
                r.is_approved,
95
                r.is_pinned,
96
                r.terms,
97
                GROUP_CONCAT(DISTINCT CONCAT(p1.post_type, ':', p1.post_name)) AS assigned_posts,
98
                GROUP_CONCAT(DISTINCT att.term_id) AS assigned_terms,
99
                GROUP_CONCAT(DISTINCT aut.user_id) AS assigned_users,
100
                GROUP_CONCAT(DISTINCT pm.meta_value) as response
101
            FROM {$this->ratingsTable} AS r
102
            INNER JOIN {$this->db->posts} AS p ON r.review_id = p.ID
103
            LEFT JOIN {$this->assignedPostsTable} AS apt ON r.ID = apt.rating_id
104
            LEFT JOIN {$this->assignedTermsTable} AS att ON r.ID = att.rating_id
105
            LEFT JOIN {$this->assignedUsersTable} AS aut ON r.ID = aut.rating_id
106
            LEFT JOIN {$this->db->posts} AS p1 ON apt.post_id = p1.ID
107
            LEFT JOIN gl_postmeta AS pm ON (r.review_id = pm.post_id AND pm.meta_key = '_response')
108
            WHERE p.post_type = '{$this->postType}'
109
            AND p.post_status IN ('publish','pending')
110
            GROUP BY r.ID
111
        ");
112
    }
113
}
114