Passed
Push — master ( 6b35fa...ccb079 )
by Paul
10:24 queued 05:13
created

Style   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Test Coverage

Coverage 28.57%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 54
dl 0
loc 141
ccs 16
cts 56
cp 0.2857
rs 10
c 2
b 1
f 0
wmc 18

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setConfig() 0 9 1
A paginationArgs() 0 3 1
A generatePossibleViews() 0 13 1
A filterView() 0 19 4
A modifyField() 0 6 3
A customize() 0 10 2
A get() 0 3 1
A isPublicInstance() 0 10 4
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Database\OptionManager;
6
use GeminiLabs\SiteReviews\Defaults\PaginationDefaults;
7
use GeminiLabs\SiteReviews\Defaults\StyleFieldsDefaults;
8
use GeminiLabs\SiteReviews\Defaults\StyleValidationDefaults;
9
use GeminiLabs\SiteReviews\Helpers\Arr;
10
use GeminiLabs\SiteReviews\Helpers\Str;
11
use GeminiLabs\SiteReviews\Modules\Html\Builder;
12
13
class Style
14
{
15
    /**
16
     * @var array
17
     */
18
    public $fields;
19
20
    /**
21
     * @var string
22
     */
23
    public $style;
24
25
    /**
26
     * @var array
27
     */
28
    public $pagination;
29
30
    /**
31
     * @var array
32
     */
33
    public $validation;
34
35 7
    public function __construct()
36
    {
37 7
        $this->style = glsr(OptionManager::class)->get('settings.general.style', 'default');
38 7
        $this->setConfig();
39 7
    }
40
41
    /**
42
     * @param string $view
43
     * @return string
44
     */
45 7
    public function filterView($view)
46
    {
47
        $styledViews = [
48 7
            'templates/form/field',
49
            'templates/form/response',
50
            'templates/form/submit-button',
51
            'templates/reviews-form',
52
        ];
53 7
        if (!preg_match('('.implode('|', $styledViews).')', $view)) {
54 7
            return $view;
55
        }
56
        $views = $this->generatePossibleViews($view);
57
        foreach ($views as $possibleView) {
58
            if (!file_exists(glsr()->file($possibleView))) {
59
                continue;
60
            }
61
            return Str::removePrefix('views/', $possibleView);
62
        }
63
        return $view;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function get()
70
    {
71
        return apply_filters('site-reviews/style', $this->style);
72
    }
73
74
    /**
75
     * @return array
76
     */
77 7
    public function setConfig()
78
    {
79 7
        $config = shortcode_atts(
80 7
            array_fill_keys(['fields', 'pagination', 'validation'], []),
81 7
            glsr()->config('styles/'.$this->style)
82
        );
83 7
        $this->fields = glsr(StyleFieldsDefaults::class)->restrict($config['fields']);
84 7
        $this->pagination = glsr(PaginationDefaults::class)->restrict($config['pagination']);
85 7
        $this->validation = glsr(StyleValidationDefaults::class)->restrict($config['validation']);
86 7
    }
87
88
    /**
89
     * @return void
90
     */
91
    public function modifyField(Builder $instance)
92
    {
93
        if (!$this->isPublicInstance($instance) || empty(array_filter($this->fields))) {
94
            return;
95
        }
96
        call_user_func_array([$this, 'customize'], [$instance]);
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function paginationArgs(array $args)
103
    {
104
        return wp_parse_args($args, $this->pagination);
105
    }
106
107
    /**
108
     * @return void
109
     */
110
    protected function customize(Builder $instance)
111
    {
112
        if (!array_key_exists($instance->tag, $this->fields)) {
113
            return;
114
        }
115
        $args = wp_parse_args($instance->args, array_fill_keys(['class', 'type'], ''));
116
        $key = $instance->tag.'_'.$args['type'];
117
        $classes = Arr::get($this->fields, $key, Arr::get($this->fields, $instance->tag));
118
        $instance->args['class'] = trim($args['class'].' '.$classes);
119
        do_action_ref_array('site-reviews/customize/'.$this->style, [$instance]);
120
    }
121
122
    /**
123
     * @param string $view
124
     * @return array
125
     */
126
    protected function generatePossibleViews($view)
127
    {
128
        $basename = basename($view);
129
        $basepath = rtrim($view, $basename);
130
        $customPath = 'views/partials/styles/'.$this->style.'/';
131
        $parts = explode('_', $basename);
132
        $views = [
133
            $customPath.$basename,
134
            $customPath.$parts[0],
135
            $view,
136
            $basepath.$parts[0],
137
        ];
138
        return array_filter($views);
139
    }
140
141
    /**
142
     * @return bool
143
     */
144
    protected function isPublicInstance(Builder $instance)
145
    {
146
        $args = wp_parse_args($instance->args, [
147
            'is_public' => false,
148
            'is_raw' => false,
149
        ]);
150
        if (is_admin() || !$args['is_public'] || $args['is_raw']) {
151
            return false;
152
        }
153
        return true;
154
    }
155
}
156