Test Failed
Push — develop ( c6b569...7a7654 )
by Paul
09:40
created

SiteReviewsSummaryShortcode::config()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 73
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1.216

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 73
ccs 8
cts 20
cp 0.4
rs 8.8945
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1.216

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Sanitizer;
10
use GeminiLabs\SiteReviews\Modules\Schema;
11
12
class SiteReviewsSummaryShortcode extends Shortcode
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $ratings;
18
19
    public function buildTemplate(): string
20
    {
21
        $this->ratings = glsr(RatingManager::class)->ratings($this->args);
22
        $this->debug(['ratings' => $this->ratings]);
23
        if ($this->isEmpty()) {
24
            return glsr()->filterString('summary/if_empty', '');
25
        }
26
        $this->generateSchema();
27
        return glsr(Template::class)->build('templates/reviews-summary', [
28
            'args' => $this->args,
29
            'context' => [
30
                'class' => $this->getClasses(),
31
                'id' => '', // @deprecated in v5.0
32
                'percentages' => $this->buildTemplateTag('percentages'),
33
                'rating' => $this->buildTemplateTag('rating'),
34
                'stars' => $this->buildTemplateTag('stars'),
35
                'text' => $this->buildTemplateTag('text'),
36
            ],
37
        ]);
38
    }
39
40
    protected function buildTemplateTag(string $tag): string
41
    {
42
        $args = $this->args;
43
        $className = Helper::buildClassName(['summary', $tag, 'tag'], 'Modules\Html\Tags');
44
        $className = glsr()->filterString("summary/tag/{$tag}", $className, $this);
45
        $field = class_exists($className)
46
            ? glsr($className, compact('tag', 'args'))->handleFor('summary', null, $this->ratings)
47
            : '';
48
        return glsr()->filterString("summary/build/{$tag}", $field, $this->ratings, $this);
49
    }
50
51
    protected function config(): array
52
    {
53
        return [
54
            'assigned_posts' => [
55
                'label' => esc_html_x('Limit Reviews by Assigned Pages', 'admin-text', 'site-reviews'),
56
                'placeholder' => esc_html_x('Select a Page...', 'admin-text', 'site-reviews'),
57
                'type' => 'select',
58
            ],
59
            'assigned_terms' => [
60
                'label' => esc_html_x('Limit Reviews by Categories', 'admin-text', 'site-reviews'),
61
                'placeholder' => esc_html_x('Select a Category...', 'admin-text', 'site-reviews'),
62
                'type' => 'select',
63
            ],
64
            'assigned_users' => [
65
                'label' => esc_html_x('Limit Reviews by Assigned Users', 'admin-text', 'site-reviews'),
66
                'placeholder' => esc_html_x('Select a User...', 'admin-text', 'site-reviews'),
67
                'type' => 'select',
68 8
            ],
69
            'terms' => [
70 8
                'label' => esc_html_x('Limit Reviews by Accepted Terms', 'admin-text', 'site-reviews'),
71 8
                'options' => [
72 8
                    'true' => esc_html_x('Terms were accepted', 'admin-text', 'site-reviews'),
73 8
                    'false' => esc_html_x('Terms were not accepted', 'admin-text', 'site-reviews'),
74 8
                ],
75 8
                'placeholder' => esc_html_x('Select Review Terms...', 'admin-text', 'site-reviews'),
76 8
                'type' => 'select',
77
            ],
78
            'type' => [
79
                'label' => esc_html_x('Limit Reviews by Type', 'admin-text', 'site-reviews'),
80
                'options' => $this->getTypeOptions(),
81
                'placeholder' => esc_html_x('Select a Review Type...', 'admin-text', 'site-reviews'),
82
                'type' => 'select',
83
            ],
84
            'rating' => [
85
                'default' => (string) $this->minRating(),
86
                'group' => 'display',
87
                'label' => esc_html_x('Minimum Rating', 'admin-text', 'site-reviews'),
88
                'max' => $this->maxRating(),
89
                'min' => $this->minRating(),
90
                'placeholder' => (string) $this->minRating(),
91
                'step' => 1,
92
                'type' => 'number',
93
            ],
94
            'rating_field' => [
95
                'description' => sprintf(_x('Use the %sReview Forms%s addon to add custom rating fields.', 'admin-text', 'site-reviews'),
96
                    '<a href="https://niftyplugins.com/plugins/site-reviews-forms/" target="_blank">', '</a>'
97
                ),
98
                'label' => esc_html_x('Custom Rating Field Name', 'admin-text', 'site-reviews'),
99
                'group' => 'display',
100
                'type' => 'text',
101
            ],
102
            'schema' => [
103
                'description' => esc_html_x('The schema should only be enabled once on your page.', 'admin-text', 'site-reviews'),
104
                'group' => 'schema',
105
                'label' => esc_html_x('Enable the schema?', 'admin-text', 'site-reviews'),
106
                'type' => 'checkbox',
107
            ],
108
            'hide' => [
109
                'group' => 'hide',
110
                'options' => $this->getHideOptions(),
111
                'type' => 'checkbox',
112
            ],
113
            'id' => [
114
                'description' => esc_html_x('This should be a unique value.', 'admin-text', 'site-reviews'),
115
                'group' => 'advanced',
116
                'label' => esc_html_x('Custom ID', 'admin-text', 'site-reviews'),
117
                'type' => 'text',
118
            ],
119
            'class' => [
120
                'description' => esc_html_x('Separate multiple classes with spaces.', 'admin-text', 'site-reviews'),
121
                'group' => 'advanced',
122
                'label' => esc_html_x('Additional CSS classes', 'admin-text', 'site-reviews'),
123
                'type' => 'text',
124
            ],
125
        ];
126
    }
127
128
    protected function generateSchema(): void
129
    {
130
        if (Cast::toBool($this->args['schema'])) {
131
            glsr(Schema::class)->store(
132
                glsr(Schema::class)->buildSummary($this->args, $this->ratings)
133
            );
134
        }
135
    }
136
137
    protected function getClasses(): string
138
    {
139
        $classes = ['glsr-summary'];
140
        $classes[] = $this->args['class'];
141
        $classes = implode(' ', $classes);
142
        return glsr(Sanitizer::class)->sanitizeAttrClass($classes);
143
    }
144
145
    protected function hideOptions(): array
146
    {
147
        return [
148
            'rating' => _x('Hide the rating', 'admin-text', 'site-reviews'),
149
            'stars' => _x('Hide the stars', 'admin-text', 'site-reviews'),
150
            'summary' => _x('Hide the summary', 'admin-text', 'site-reviews'),
151
            'bars' => _x('Hide the percentage bars', 'admin-text', 'site-reviews'),
152
            'if_empty' => _x('Hide if no reviews are found', 'admin-text', 'site-reviews'),
153
        ];
154
    }
155
156
    protected function isEmpty(): bool
157
    {
158
        return !array_sum($this->ratings) && in_array('if_empty', $this->args['hide']);
159
    }
160
161
    protected function shortcodeDescription(): string
162
    {
163
        return esc_html_x('Display a rating summary', 'admin-text', 'site-reviews');
164
    }
165
166
    protected function shortcodeName(): string
167
    {
168
        return esc_html_x('Rating Summary', 'admin-text', 'site-reviews');
169
    }
170
}
171