Test Failed
Push — develop ( 66133f...833c7b )
by Paul
06:57
created

ReviewsHtml::getClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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 getPagination(bool $wrap = true): string
50
    {
51
        $html = glsr(Partial::class)->build('pagination', [
52
            'add_args' => $this->args->pageUrlParameters,
53
            'baseUrl' => $this->args->pageUrl,
54
            'current' => $this->args->page,
55
            'total' => $this->max_num_pages,
56
            'type' => $this->args->pagination, // we use this to pass the pagination setting
57
        ]);
58
        if (!$wrap || empty($html)) { // only display the pagination when it's needed
59
            return $html;
60
        }
61
        $classes = 'glsr-pagination';
62
        if ('ajax' === $this->args->pagination) {
63
            $classes .= ' glsr-ajax-pagination';
64
        }
65
        if ('loadmore' === $this->args->pagination) {
66
            $classes .= ' glsr-ajax-loadmore';
67
        }
68
        $dataAttributes = glsr(SiteReviewsDefaults::class)->dataAttributes($this->args->toArray());
69
        return glsr(Builder::class)->div(wp_parse_args([
70
            'class' => $classes,
71
            'data-id' => $this->args->id,
72
            'text' => $html,
73
        ], $dataAttributes));
74
    }
75
76
    public function getReviews(): string
77
    {
78
        return empty($this->rendered)
79
            ? $this->fallback
80
            : implode(PHP_EOL, $this->rendered);
81
    }
82
83
    /**
84
     * @param string $key
85
     *
86
     * @return mixed
87
     */
88
    #[\ReturnTypeWillChange]
89
    public function offsetGet($key)
90
    {
91
        if ('attributes' === $key) {
92
            if (empty($this->attributes)) {
93
                $this->attributes = $this->reviews->attributes();
94
            }
95
            return glsr(Attributes::class)->div($this->attributes)->toString();
96
        }
97
        if (array_key_exists($key, $this->rendered)) {
98
            return $this->rendered[$key];
99
        }
100
        if (in_array($key, ['navigation', 'pagination'])) { // @deprecated_v5 (navigation)
101
            return $this->getPagination();
102
        }
103
        return property_exists($this, $key)
104
            ? $this->$key
105
            : glsr()->filterString("reviews/html/{$key}", null, $this);
106
    }
107
108
    protected function getReviewsFallback(): string
109
    {
110
        if (empty($this->args->fallback) && glsr(OptionManager::class)->getBool('settings.reviews.fallback')) {
111
            $this->args->fallback = __('There are no reviews yet. Be the first one to write one.', 'site-reviews');
112
        }
113
        $fallback = glsr(Builder::class)->p([
114
            'class' => 'glsr-no-margins',
115
            'text' => $this->args->fallback,
116
        ]);
117
        return glsr()->filterString('reviews/fallback', $fallback, $this->args->toArray());
118
    }
119
120
    protected function renderReviews(Reviews $reviews): array
121
    {
122
        $rendered = [];
123
        foreach ($reviews as $review) {
124
            $rendered[] = $review->build($this->args->toArray());
125
        }
126
        return $rendered;
127
    }
128
}
129