Passed
Push — master ( 64f340...ccb079 )
by Paul
08:17 queued 03:57
created

TermCountsManager::updateAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database;
7
use GeminiLabs\SiteReviews\Modules\Rating;
8
use GeminiLabs\SiteReviews\Review;
9
10
class TermCountsManager
11
{
12
    /**
13
     * @var CountsManager
14
     */
15
    protected $manager;
16
17 1
    public function __construct()
18
    {
19 1
        $this->manager = glsr(CountsManager::class);
20 1
    }
21
22
    /**
23
     * @param int $termTaxonomyId
24
     * @return array
25
     */
26
    public function build($termTaxonomyId)
27
    {
28
        return $this->manager->buildCounts([
29
            'term_ids' => [$termTaxonomyId],
30
        ]);
31
    }
32
33
    /**
34
     * @return void
35
     */
36
    public function decrease(Review $review)
37
    {
38
        foreach ($review->term_ids as $termId) {
39
            if (empty($counts = $this->get($termId))) {
40
                continue;
41
            }
42
            $this->update($termId,
43
                $this->manager->decreaseRating($counts, $review->review_type, $review->rating)
44
            );
45
        }
46
    }
47
48
    /**
49
     * @param int $termId
50
     * @return array
51
     */
52
    public function get($termId)
53
    {
54
        return array_filter((array) get_term_meta($termId, CountsManager::META_COUNT, true));
55
    }
56
57
    /**
58
     * @return void
59
     */
60
    public function increase(Review $review)
61
    {
62
        $terms = glsr(ReviewManager::class)->normalizeTerms(implode(',', $review->term_ids));
63
        foreach ($terms as $term) {
64
            $counts = $this->get($term['term_id']);
65
            $counts = empty($counts)
66
                ? $this->build($term['term_taxonomy_id'])
67
                : $this->manager->increaseRating($counts, $review->review_type, $review->rating);
68
            $this->update($term['term_id'], $counts);
69
        }
70
    }
71
72
    /**
73
     * @param int $termId
74
     * @return void
75
     */
76
    public function update($termId, array $reviewCounts)
77
    {
78
        $term = get_term($termId, Application::TAXONOMY);
79
        if (!isset($term->term_id)) {
80
            return;
81
        }
82
        $ratingCounts = $this->manager->flatten($reviewCounts);
83
        update_term_meta($termId, CountsManager::META_COUNT, $reviewCounts);
84
        update_term_meta($termId, CountsManager::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts));
85
        update_term_meta($termId, CountsManager::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts));
86
    }
87
88
    /**
89
     * @return void
90
     */
91 1
    public function updateAll()
92
    {
93 1
        glsr(SqlQueries::class)->deleteTermCountMetaKeys();
94 1
        $terms = glsr(Database::class)->getTerms([
95 1
            'fields' => 'all',
96
        ]);
97 1
        foreach ($terms as $term) {
98
            $this->update($term->term_id, $this->build($term->term_taxonomy_id));
99
        }
100 1
    }
101
}
102