Passed
Push — master ( 2da942...d633d7 )
by Paul
15:03 queued 05:40
created

CountManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 71.93%

Importance

Changes 0
Metric Value
wmc 11
eloc 47
dl 0
loc 124
ccs 41
cts 57
cp 0.7193
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A users() 0 6 1
A posts() 0 6 1
A terms() 0 6 1
A recalculate() 0 5 1
A metaId() 0 3 1
A recalculateFor() 0 10 2
A metaTable() 0 3 1
A metaType() 0 3 1
A ratingValuesForInsert() 0 23 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Database;
6
use GeminiLabs\SiteReviews\Helpers\Cast;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
use GeminiLabs\SiteReviews\Modules\Rating;
9
10
class CountManager
11
{
12
    const META_AVERAGE = '_glsr_average';
13
    const META_RANKING = '_glsr_ranking';
14
    const META_REVIEWS = '_glsr_reviews';
15
16
    /**
17
     * @param int $postId
18
     * @return void
19
     */
20 2
    public function posts($postId)
21
    {
22 2
        $counts = glsr_get_ratings(['assigned_posts' => $postId]);
23 2
        update_post_meta($postId, static::META_AVERAGE, $counts->average);
24 2
        update_post_meta($postId, static::META_RANKING, $counts->ranking);
25 2
        update_post_meta($postId, static::META_REVIEWS, $counts->reviews);
26 2
    }
27
28
    /**
29
     * @return void
30
     */
31 7
    public function recalculate()
32
    {
33 7
        $this->recalculateFor('post');
34 7
        $this->recalculateFor('term');
35 7
        $this->recalculateFor('user');
36 7
    }
37
38
    /**
39
     * @param string $type
40
     * @return void
41
     */
42 7
    public function recalculateFor($type)
43
    {
44 7
        $metaKeys = [static::META_AVERAGE, static::META_RANKING, static::META_REVIEWS];
45 7
        $metaTable = $this->metaTable($type);
46 7
        glsr(Database::class)->deleteMeta($metaKeys, $metaTable);
47 7
        if ($values = $this->ratingValuesForInsert($type)) {
48
            glsr(Database::class)->insertBulk($metaTable, $values, [
49
                $this->metaId($type),
50
                'meta_key',
51
                'meta_value',
52
            ]);
53
        }
54 7
    }
55
56
    /**
57
     * @param int $termId
58
     * @return void
59
     */
60 2
    public function terms($termId)
61
    {
62 2
        $counts = glsr_get_ratings(['assigned_terms' => $termId]);
63 2
        update_term_meta($termId, static::META_AVERAGE, $counts->average);
64 2
        update_term_meta($termId, static::META_RANKING, $counts->ranking);
65 2
        update_term_meta($termId, static::META_REVIEWS, $counts->reviews);
66 2
    }
67
68
    /**
69
     * @param int $userId
70
     * @return void
71
     */
72 2
    public function users($userId)
73
    {
74 2
        $counts = glsr_get_ratings(['assigned_users' => $userId]);
75 2
        update_user_meta($userId, static::META_AVERAGE, $counts->average);
76 2
        update_user_meta($userId, static::META_RANKING, $counts->ranking);
77 2
        update_user_meta($userId, static::META_REVIEWS, $counts->reviews);
78 2
    }
79
80
    /**
81
     * @param string $type
82
     * @return string
83
     */
84 7
    protected function metaId($type)
85
    {
86 7
        return $this->metaType($type).'_id';
87
    }
88
89
    /**
90
     * @param string $type
91
     * @return string
92
     */
93 7
    protected function metaTable($type)
94
    {
95 7
        return $this->metaType($type).'meta';
96
    }
97
98
    /**
99
     * @param string $type
100
     * @return string
101
     */
102 7
    protected function metaType($type)
103
    {
104 7
        return Str::restrictTo(['post', 'term', 'user'], Cast::toString($type), 'post');
105
    }
106
107
    /**
108
     * @param string $metaType
109
     * @return array
110
     */
111 7
    protected function ratingValuesForInsert($metaType)
112
    {
113 7
        $metaId = $this->metaId($metaType);
114 7
        $ratings = glsr(RatingManager::class)->ratingsGroupedBy($metaType);
115 7
        $values = [];
116 7
        foreach ($ratings as $id => $counts) {
117
            $values[] = [
118
                $metaId => $id,
119
                'meta_key' => static::META_AVERAGE,
120
                'meta_value' => glsr(Rating::class)->average($counts),
121
            ];
122
            $values[] = [
123
                $metaId => $id,
124
                'meta_key' => static::META_RANKING,
125
                'meta_value' => glsr(Rating::class)->ranking($counts),
126
            ];
127
            $values[] = [
128
                $metaId => $id,
129
                'meta_key' => static::META_REVIEWS,
130
                'meta_value' => array_sum($counts),
131
            ];
132
        }
133 7
        return $values;
134
    }
135
}
136