Passed
Push — master ( 817cab...57b58d )
by Paul
09:30 queued 02:21
created

SiteReviewsSummary::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
nc 2
nop 1
dl 0
loc 17
ccs 0
cts 17
cp 0
crap 6
rs 9.7998
c 1
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html\Partials;
4
5
use GeminiLabs\SiteReviews\Contracts\PartialContract;
6
use GeminiLabs\SiteReviews\Database\RatingManager;
7
use GeminiLabs\SiteReviews\Helper;
8
use GeminiLabs\SiteReviews\Helpers\Cast;
9
use GeminiLabs\SiteReviews\Modules\Html\Template;
10
use GeminiLabs\SiteReviews\Modules\Schema;
11
12
class SiteReviewsSummary implements PartialContract
13
{
14
    /**
15
     * @var array
16
     */
17
    public $args;
18
19
    /**
20
     * @var array
21
     */
22
    protected $ratings;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function build(array $args = [])
28
    {
29
        $this->args = $args;
30
        $this->ratings = glsr(RatingManager::class)->ratings($args);
31
        if ($this->isEmpty()) {
32
            return;
33
        }
34
        $this->generateSchema();
35
        return glsr(Template::class)->build('templates/reviews-summary', [
36
            'args' => $this->args,
37
            'context' => [
38
                'class' => $this->getClass(),
39
                'id' => '', // @deprecated in v5.0
40
                'percentages' => $this->buildTemplateTag('percentages'),
41
                'rating' => $this->buildTemplateTag('rating'),
42
                'stars' => $this->buildTemplateTag('stars'),
43
                'text' => $this->buildTemplateTag('text'),
44
            ],
45
        ]);
46
    }
47
48
    /**
49
     * @param string $tag
50
     * @return string
51
     */
52
    protected function buildTemplateTag($tag)
53
    {
54
        $args = $this->args;
55
        $classname = implode('-', ['summary', $tag, 'tag']);
56
        $className = Helper::buildClassName($classname, 'Modules\Html\Tags');
57
        $field = class_exists($className)
58
            ? glsr($className, compact('tag', 'args'))->handleFor('summary', null, $this->ratings)
59
            : null;
60
        return glsr()->filterString('summary/build/'.$tag, $field, $this->ratings, $this);
61
    }
62
63
    /**
64
     * @return void
65
     */
66
    protected function generateSchema()
67
    {
68
        if (Cast::toBool($this->args['schema'])) {
69
            glsr(Schema::class)->store(
70
                glsr(Schema::class)->buildSummary($this->args, $this->ratings)
71
            );
72
        }
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    protected function getClass()
79
    {
80
        return trim('glsr-summary '.$this->args['class']);
81
    }
82
83
    /**
84
     * @return bool
85
     */
86
    protected function isEmpty()
87
    {
88
        return !array_sum($this->ratings) && in_array('if_empty', $this->args['hide']);
89
    }
90
}
91