Test Failed
Push — develop ( 7aea1a...9fd874 )
by Paul
15:27
created

ReviewsHtml   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 20
eloc 67
c 2
b 0
f 1
dl 0
loc 117
rs 10
ccs 0
cts 71
cp 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __toString() 0 14 1
A renderReviews() 0 7 2
A getReviewsFallback() 0 10 3
A getPagination() 0 25 5
A offsetGet() 0 12 4
A getReviews() 0 5 2
A attributes() 0 6 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html;
4
5
use GeminiLabs\SiteReviews\Arguments;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Defaults\SiteReviewsDefaults;
8
use GeminiLabs\SiteReviews\Helper;
9
use GeminiLabs\SiteReviews\Modules\Sanitizer;
10
use GeminiLabs\SiteReviews\Reviews;
11
12
class ReviewsHtml extends \ArrayObject
13
{
14
    public Arguments $args;
15
    public string $fallback;
16
    public int $max_num_pages;
17
    public Reviews $reviews;
18
    public array $rendered;
19
20
    protected array $attributes = [];
21
22
    public function __construct(Reviews $reviews)
23
    {
24
        $this->args = glsr()->args($reviews->args);
25
        $this->fallback = $this->getReviewsFallback();
26
        $this->max_num_pages = $reviews->max_num_pages;
27
        $this->reviews = $reviews;
28
        $this->rendered = $this->renderReviews($reviews);
29
        parent::__construct($this->reviews, \ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS);
30
    }
31
32
    public function __toString(): string
33
    {
34
        return glsr(Template::class)->build('templates/reviews', [
35
            'args' => $this->args,
36
            'context' => [
37
                'assigned_to' => $this->args->assigned_posts,
38
                'category' => $this->args->assigned_terms,
39
                'class' => 'glsr-reviews',
40
                'id' => '', // @deprecated_v5
41
                'pagination' => Helper::ifTrue(!empty($this->args->pagination), $this->getPagination()),
42
                'reviews' => $this->getReviews(),
43
            ],
44
            'fallback' => $this->fallback,
45
            'reviews' => $this->reviews,
46
        ]);
47
    }
48
49
    public function attributes(): array
50
    {
51
        if (empty($this->attributes)) {
52
            $this->attributes = $this->reviews->attributes();
53
        }
54
        return $this->attributes;
55
    }
56
57
    public function getPagination(bool $wrap = true): string
58
    {
59
        $html = glsr(Partial::class)->build('pagination', [
60
            'add_args' => $this->args->pageUrlParameters,
61
            'baseUrl' => $this->args->pageUrl,
62
            'current' => $this->args->page,
63
            'total' => $this->max_num_pages,
64
            'type' => $this->args->pagination, // we use this to pass the pagination setting
65
        ]);
66
        if (!$wrap || empty($html)) { // only display the pagination when it's needed
67
            return $html;
68
        }
69
        $classes = 'glsr-pagination';
70
        if ('ajax' === $this->args->pagination) {
71
            $classes .= ' glsr-ajax-pagination';
72
        }
73
        if ('loadmore' === $this->args->pagination) {
74
            $classes .= ' glsr-ajax-loadmore';
75
        }
76
        $dataAttributes = glsr(SiteReviewsDefaults::class)->dataAttributes($this->args->toArray());
77
        return glsr(Builder::class)->div(wp_parse_args([
78
            'class' => $classes,
79
            'data-id' => $this->args->id,
80
            'text' => $html,
81
        ], $dataAttributes));
82
    }
83
84
    public function getReviews(): string
85
    {
86
        return empty($this->rendered)
87
            ? $this->fallback
88
            : implode(PHP_EOL, $this->rendered);
89
    }
90
91
    /**
92
     * @param string $key
93
     *
94
     * @return mixed
95
     */
96
    #[\ReturnTypeWillChange]
97
    public function offsetGet($key)
98
    {
99
        if ('attributes' === $key) {
100
            return glsr(Attributes::class)->div($this->attributes())->toString();
101
        }
102
        if (array_key_exists($key, $this->rendered)) {
103
            return $this->rendered[$key];
104
        }
105
        return property_exists($this, $key)
106
            ? $this->$key
107
            : glsr()->filterString("reviews/html/{$key}", null, $this);
108
    }
109
110
    protected function getReviewsFallback(): string
111
    {
112
        if (empty($this->args->fallback) && glsr(OptionManager::class)->getBool('settings.reviews.fallback')) {
113
            $this->args->fallback = __('There are no reviews yet. Be the first one to write one.', 'site-reviews');
114
        }
115
        $fallback = glsr(Builder::class)->p([
116
            'class' => 'glsr-no-margins',
117
            'text' => $this->args->fallback,
118
        ]);
119
        return glsr()->filterString('reviews/fallback', $fallback, $this->args->toArray());
120
    }
121
122
    protected function renderReviews(Reviews $reviews): array
123
    {
124
        $rendered = [];
125
        foreach ($reviews as $review) {
126
            $rendered[] = $review->build($this->args->toArray());
127
        }
128
        return $rendered;
129
    }
130
}
131