Passed
Push — master ( 29cd16...a170f1 )
by Paul
22:00 queued 10:57
created

StarRating::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2.0006

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
c 2
b 0
f 0
dl 0
loc 21
ccs 17
cts 18
cp 0.9444
rs 9.6666
cc 2
nc 2
nop 1
crap 2.0006
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html\Partials;
4
5
use GeminiLabs\SiteReviews\Contracts\PartialContract;
6
use GeminiLabs\SiteReviews\Defaults\StarRatingDefaults;
7
use GeminiLabs\SiteReviews\Modules\Html\Template;
8
use GeminiLabs\SiteReviews\Modules\Rating;
9
10
class StarRating implements PartialContract
11
{
12
    /**
13
     * @var \GeminiLabs\SiteReviews\Arguments
14
     */
15
    public $data;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20 6
    public function build(array $data = [])
21
    {
22 6
        $this->data = glsr()->args(glsr(StarRatingDefaults::class)->merge($data));
23 6
        $maxRating = glsr()->constant('MAX_RATING', Rating::class);
24 6
        $fullStars = intval(floor($this->data->rating));
25 6
        $halfStars = intval(ceil($this->data->rating - $fullStars));
26 6
        $emptyStars = max(0, $maxRating - $fullStars - $halfStars);
27 6
        $title = $this->data->count > 0
28
            ? __('Rated <strong>%s</strong> out of %s based on %s ratings', 'site-reviews')
29 6
            : __('Rated <strong>%s</strong> out of %s', 'site-reviews');
30 6
        return glsr(Template::class)->build('templates/rating/stars', [
31 6
            'args' => glsr()->args($this->data->args),
32
            'context' => [
33 6
                'empty_stars' => $this->getTemplate('empty-star', $emptyStars),
34 6
                'full_stars' => $this->getTemplate('full-star', $fullStars),
35 6
                'half_stars' => $this->getTemplate('half-star', $halfStars),
36 6
                'prefix' => $this->data->prefix,
37 6
                'rating' => $this->data->rating,
38 6
                'title' => sprintf($title, $this->data->rating, $maxRating, $this->data->count),
39
            ],
40 6
            'partial' => $this,
41
        ]);
42
    }
43
44
    /**
45
     * @param string $templateName
46
     * @param int $timesRepeated
47
     * @return string
48
     */
49 6
    protected function getTemplate($templateName, $timesRepeated)
50
    {
51 6
        $template = glsr(Template::class)->build('templates/rating/'.$templateName, [
52 6
            'args' => $this->data->args,
53
            'context' => [
54 6
                'prefix' => $this->data->prefix,
55
            ],
56 6
            'partial' => $this,
57
        ]);
58 6
        return str_repeat($template, $timesRepeated);
59
    }
60
}
61