Passed
Push — master ( 4f783c...449b2c )
by Paul
07:14
created

SiteReviewsSummaryShortcode   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 16.22%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 10
eloc 35
c 3
b 0
f 1
dl 0
loc 91
ccs 6
cts 37
cp 0.1622
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getClasses() 0 3 1
A generateSchema() 0 5 2
A buildTemplate() 0 17 2
A isEmpty() 0 3 2
A hideOptions() 0 8 1
A buildTemplateTag() 0 9 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Shortcodes;
4
5
use GeminiLabs\SiteReviews\Database\RatingManager;
6
use GeminiLabs\SiteReviews\Helper;
7
use GeminiLabs\SiteReviews\Helpers\Cast;
8
use GeminiLabs\SiteReviews\Modules\Html\Template;
9
use GeminiLabs\SiteReviews\Modules\Schema;
10
11
class SiteReviewsSummaryShortcode extends Shortcode
12
{
13
    /**
14
     * @var array
15
     */
16
    public $args;
17
18
    /**
19
     * @var array
20
     */
21
    protected $ratings;
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function buildTemplate(array $args = [])
27
    {
28
        $this->args = $args;
29
        $this->ratings = glsr(RatingManager::class)->ratings($args);
30
        if ($this->isEmpty()) {
31
            return;
32
        }
33
        $this->generateSchema();
34
        return glsr(Template::class)->build('templates/reviews-summary', [
35
            'args' => $this->args,
36
            'context' => [
37
                'class' => $this->getClasses(),
38
                'id' => '', // @deprecated in v5.0
39
                'percentages' => $this->buildTemplateTag('percentages'),
40
                'rating' => $this->buildTemplateTag('rating'),
41
                'stars' => $this->buildTemplateTag('stars'),
42
                'text' => $this->buildTemplateTag('text'),
43
            ],
44
        ]);
45
    }
46
47
    /**
48
     * @param string $tag
49
     * @return string
50
     */
51
    protected function buildTemplateTag($tag)
52
    {
53
        $args = $this->args;
54
        $className = Helper::buildClassName(['summary', $tag, 'tag'], 'Modules\Html\Tags');
55
        $className = glsr()->filterString('summary/tag/'.$tag, $className, $this);
56
        $field = class_exists($className)
57
            ? glsr($className, compact('tag', 'args'))->handleFor('summary', null, $this->ratings)
58
            : null;
59
        return glsr()->filterString('summary/build/'.$tag, $field, $this->ratings, $this);
60
    }
61
62
    /**
63
     * @return void
64
     */
65
    protected function generateSchema()
66
    {
67
        if (Cast::toBool($this->args['schema'])) {
68
            glsr(Schema::class)->store(
69
                glsr(Schema::class)->buildSummary($this->args, $this->ratings)
70
            );
71
        }
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    protected function getClasses()
78
    {
79
        return trim('glsr-summary '.$this->args['class']);
80
    }
81
82
    /**
83
     * @return array
84
     */
85 7
    protected function hideOptions()
86
    {
87
        return [
88 7
            'rating' => _x('Hide the rating', 'admin-text', 'site-reviews'),
89 7
            'stars' => _x('Hide the stars', 'admin-text', 'site-reviews'),
90 7
            'summary' => _x('Hide the summary', 'admin-text', 'site-reviews'),
91 7
            'bars' => _x('Hide the percentage bars', 'admin-text', 'site-reviews'),
92 7
            'if_empty' => _x('Hide if no reviews are found', 'admin-text', 'site-reviews'),
93
        ];
94
    }
95
96
    /**
97
     * @return bool
98
     */
99
    protected function isEmpty()
100
    {
101
        return !array_sum($this->ratings) && in_array('if_empty', $this->args['hide']);
102
    }
103
}
104