Passed
Push — master ( 41584c...c36188 )
by Paul
12:21
created

StarRating   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 96.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 30
c 1
b 0
f 0
dl 0
loc 58
ccs 26
cts 27
cp 0.963
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTemplate() 0 8 1
A build() 0 17 2
A setProperties() 0 10 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html\Partials;
4
5
use GeminiLabs\SiteReviews\Contracts\PartialContract;
6
use GeminiLabs\SiteReviews\Modules\Html\Template;
7
use GeminiLabs\SiteReviews\Modules\Rating;
8
9
class StarRating implements PartialContract
10
{
11
    protected $count;
12
    protected $prefix;
13
    protected $rating;
14
15
    /**
16
     * {@inheritdoc}
17
     */
18 6
    public function build(array $args = [])
19
    {
20 6
        $this->setProperties($args);
21 6
        $maxRating = glsr()->constant('MAX_RATING', Rating::class);
22 6
        $fullStars = intval(floor($this->rating));
23 6
        $halfStars = intval(ceil($this->rating - $fullStars));
24 6
        $emptyStars = max(0, $maxRating - $fullStars - $halfStars);
25 6
        $title = $this->count > 0
26
            ? __('Rated <strong>%s</strong> out of %s based on %s ratings', 'site-reviews')
27 6
            : __('Rated <strong>%s</strong> out of %s', 'site-reviews');
28 6
        return glsr(Template::class)->build('templates/rating/stars', [
29
            'context' => [
30 6
                'empty_stars' => $this->getTemplate('empty-star', $emptyStars),
31 6
                'full_stars' => $this->getTemplate('full-star', $fullStars),
32 6
                'half_stars' => $this->getTemplate('half-star', $halfStars),
33 6
                'prefix' => $this->prefix,
34 6
                'title' => sprintf($title, $this->rating, $maxRating, $this->count),
35
            ],
36
        ]);
37
    }
38
39
    /**
40
     * @param string $templateName
41
     * @param int $timesRepeated
42
     * @return string
43
     */
44 6
    protected function getTemplate($templateName, $timesRepeated)
45
    {
46 6
        $template = glsr(Template::class)->build('templates/rating/'.$templateName, [
47
            'context' => [
48 6
                'prefix' => $this->prefix,
49
            ],
50
        ]);
51 6
        return str_repeat($template, $timesRepeated);
52
    }
53
54
    /**
55
     * @return void
56
     */
57 6
    protected function setProperties(array $args)
58
    {
59 6
        $args = wp_parse_args($args, [
60 6
            'count' => 0,
61 6
            'prefix' => glsr()->isAdmin() ? '' : 'glsr-',
62 6
            'rating' => 0,
63
        ]);
64 6
        $this->count = (int) $args['count'];
65 6
        $this->prefix = $args['prefix'];
66 6
        $this->rating = (float) sprintf('%g', $args['rating']); // remove unnecessary trailing zeros
67 6
    }
68
}
69