Passed
Push — develop ( 391f8f...187ad6 )
by Paul
13:53
created

StarRating::stars()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 16
ccs 14
cts 14
cp 1
rs 9.9
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html\Partials;
4
5
use GeminiLabs\SiteReviews\Arguments;
6
use GeminiLabs\SiteReviews\Contracts\PartialContract;
7
use GeminiLabs\SiteReviews\Defaults\StarRatingDefaults;
8
use GeminiLabs\SiteReviews\Modules\Html\Builder;
9
use GeminiLabs\SiteReviews\Modules\Rating;
10
11
class StarRating implements PartialContract
12
{
13
    protected Arguments $data;
14
15 8
    public function build(array $args = []): string
16
    {
17 8
        $this->data = glsr()->args(
18 8
            glsr(StarRatingDefaults::class)->restrict($args)
19 8
        );
20 8
        return glsr(Builder::class)->div([
21 8
            'aria-label' => $this->label(),
22 8
            'class' => 'glsr-star-rating glsr-stars',
23 8
            'data-rating' => $this->data->rating,
24 8
            'data-reviews' => $this->data->reviews,
25 8
            'role' => 'img',
26 8
            'text' => $this->stars(),
27 8
        ]);
28
    }
29
30 8
    protected function label()
31
    {
32 8
        $rating = $this->data->rating;
33 8
        $title = $this->data->reviews > 0
34
            ? __('Rated %s out of %s stars based on %s ratings', 'site-reviews')
35 8
            : __('Rated %s out of %s stars', 'site-reviews');
36 8
        if (0 !== $this->data->num_half) {
37
            $rating = glsr(Rating::class)->format($rating);
38
        }
39 8
        return sprintf($title, $rating, Rating::max(), $this->data->reviews);
40
    }
41
42 8
    protected function stars(): string
43
    {
44 8
        $types = [ // order is intentional
45 8
            'full' => $this->data->num_full,
46 8
            'half' => $this->data->num_half,
47 8
            'empty' => $this->data->num_empty,
48 8
        ];
49 8
        $results = [];
50 8
        foreach ($types as $type => $repeat) {
51 8
            $template = glsr(Builder::class)->span([
52 8
                'aria-hidden' => 'true',
53 8
                'class' => "glsr-star glsr-star-{$type}",
54 8
            ]);
55 8
            $results[] = str_repeat($template, $repeat);
56
        }
57 8
        return implode('', $results);
58
    }
59
}
60