Completed
Push — master ( 990f68...9679f0 )
by Davide
06:22
created

LaravelColumns::getParametersArray()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 82
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 5.0488

Importance

Changes 8
Bugs 6 Features 2
Metric Value
eloc 40
c 8
b 6
f 2
dl 0
loc 82
ccs 35
cts 40
cp 0.875
rs 8.9688
cc 5
nc 16
nop 1
crap 5.0488

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 DavideCasiraghi\LaravelColumns;
4
5
use DavideCasiraghi\LaravelColumns\Models\Column;
6
use DavideCasiraghi\LaravelColumns\Models\ColumnGroup;
7
8
class LaravelColumns
9
{
10
    /**************************************************************************/
11
12
    /**
13
     *  Provide the column data array.
14
     *
15
     *  @param int $columnId
16
     *  @return  \DavideCasiraghi\LaravelColumns\Models\Column    $ret
17
     **/
18 2
    public static function getColumn($columnId)
19
    {
20 2
        $ret = Column::where('id', $columnId)->first();
21
22 2
        return $ret;
23
    }
24
25
    /**************************************************************************/
26
27
    /**
28
     *  Provide the columns of a specfied group.
29
     *
30
     *  @param int $columnId
31
     *  @return  \DavideCasiraghi\LaravelColumns\Models\Column    $ret
32
     **/
33 2
    public static function getColumnsByGroup($columnGroupId)
34
    {
35 2
        $ret = Column::where('columns_group', $columnGroupId)->get();
36
37 2
        return $ret;
38
    }
39
40
    /**************************************************************************/
41
42
    /**
43
     *  Provide the column group data array.
44
     *
45
     *  @param int $columnGroupId
46
     *  @return  \DavideCasiraghi\LaravelColumns\Models\ColumnGroup    $ret
47
     **/
48 2
    public static function getColumnGroup($columnGroupId)
49
    {
50 2
        $ret = ColumnGroup::where('id', $columnGroupId)->first();
51
52 2
        return $ret;
53
    }
54
55
    /**************************************************************************/
56
57
    /**
58
     *  Find the column snippet occurances in the text.
59
     *
60
     *  @param string $text
61
     *  @return array $matches
62
     **/
63 4
    public static function getColumnGroupSnippetOccurrences($text)
64
    {
65 4
        $re = '/{\#
66
            \h+column_group
67
            \h+(column_group_id)=\[([^]]*)]
68
            \h*\#}/x';
69
70 4
        if (preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0)) {
71 3
            return $matches;
72
        } else {
73 1
            return;
74
        }
75
    }
76
77
    /**************************************************************************/
78
79
    /**
80
     *  Returns the plugin parameters.
81
     *
82
     *  @param array $matches
83
     *  @return array $ret
84
     **/
85 2
    public static function getSnippetParameters($matches)
86
    {
87 2
        $ret = [];
88
89
        // Get activation string parameters (from article)
90 2
        $ret['token'] = $matches[0];
91
        //dump($matches);
92
93 2
        $ret['column_group_id'] = $matches[2];
94
95 2
        return $ret;
96
    }
97
98
    /**************************************************************************/
99
100
    /**
101
     *  Return the same text with the columns HTML replaced
102
     *  where the token strings has been found.
103
     *
104
     *  @param string $text
105
     *  @return string $ret
106
     **/
107 2
    public function replace_column_group_snippets_with_template($text)
108
    {
109 2
        $matches = self::getColumnGroupSnippetOccurrences($text);
110
        // aaaaaa
111
112 2
        if ($matches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
113 2
            foreach ($matches as $key => $single_gallery_matches) {
114 2
                $snippetParameters = self::getSnippetParameters($single_gallery_matches);
115
116 2
                $columnGroupId = $snippetParameters['column_group_id'];
117
118 2
                $columnGroup = self::getColumnGroup($columnGroupId);
119 2
                $columnGroupParameters = ($columnGroup) ? (self::getParametersArray($columnGroup)) : null;
120 2
                $columns = self::getColumnsByGroup($columnGroupId);
121
122 2
                $columnView = self::showColumnGroup($columnGroup, $columnGroupParameters, $columns);
0 ignored issues
show
Bug Best Practice introduced by
The method DavideCasiraghi\LaravelC...umns::showColumnGroup() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
                /** @scrutinizer ignore-call */ 
123
                $columnView = self::showColumnGroup($columnGroup, $columnGroupParameters, $columns);
Loading history...
123 2
                $columnHtml = $columnView->render();
124
125
                // Substitute the column html to the token that has been found
126 2
                $text = str_replace($snippetParameters['token'], $columnHtml, $text);
127
            }
128
        }
129
130 2
        $ret = $text;
131
132 2
        return $ret;
133
    }
134
135
    /***************************************************************************/
136
137
    /**
138
     * Show a Column group.
139
     *
140
     * @param  \DavideCasiraghi\LaravelColumns\Models\ColumnGroup $columnGroup
141
     * @param array $columnGroupParameters
142
     * @param  \DavideCasiraghi\LaravelColumns\Models\Column $columns
143
     * @return \Illuminate\Http\Response
144
     */
145 2
    public function showColumnGroup($columnGroup, $columnGroupParameters, $columns)
146
    {
147 2
        return view('laravel-columns::show-column-group', compact('columnGroup'))
148 2
        ->with('columnGroupParameters', $columnGroupParameters)
149 2
        ->with('columns', $columns);
150
    }
151
152
    /***************************************************************************/
153
154
    /**
155
     * Return an array with the parameters for the column.
156
     * @param  \DavideCasiraghi\LaravelColumns\Models\ColumnGroup  $columnGroup
157
     * @return array
158
     */
159 3
    public static function getParametersArray($columnGroup)
160
    {
161 3
        $container_style = 'background-color: '.$columnGroup->bkg_color.';';
162
        
163 3
        $group_title_style = 'text-align:'.$columnGroup->text_alignment.'; ';
0 ignored issues
show
Unused Code introduced by
The assignment to $group_title_style is dead and can be removed.
Loading history...
164 3
        $group_title_style = 'color:'.$columnGroup->group_title_color.'; ';
165 3
        $group_title_style .= 'font-size:'.$columnGroup->group_title_font_size.'; ';
166
        
167 3
        $group_description_style = 'text-align:'.$columnGroup->text_alignment.'; ';
168 3
        $group_button_style = 'text-align:'.$columnGroup->text_alignment.'; ';
169
        
170
        /* Wrapper style */
171 3
        $wrapper_style = 'justify-content:'.$columnGroup->justify_content.'; ';
172 3
        $wrapper_style .= 'flex-flow:'.$columnGroup->flex_flow.'; ';
173 3
        $wrapper_style .= 'flex-wrap:'.$columnGroup->flex_wrap.'; ';
174 3
        $wrapper_style .= 'text-align:'.$columnGroup->text_alignment.'; ';
175
176 3
        if ($columnGroup->background_type == 3) {
177
            $wrapper_style .= 'background-image:'.$column->background_image.'; ';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $column does not exist. Did you maybe mean $columnGroup?
Loading history...
178
            $wrapper_style .= 'background-position:'.$column->background_image_position.'; ';
179
        }
180
181
        /* Title style */
182 3
        $title_style = 'color:'.$columnGroup->column_title_color.'; ';
183 3
        $title_style .= 'font-size:'.$columnGroup->column_title_font_size.'; ';
184
185
        /* Description style */
186 3
        $description_style = 'font-size:'.$columnGroup->description_font_size.'; ';
187
188
        /* Button class*/
189 3
        $button_class = $columnGroup->button_color.'; ';
190 3
        $button_class .= $columnGroup->button_corners.'; ';
191 3
        if ($columnGroup->link_style == 3) {
192
            $button_class .= 'press-ghost; ';
193
        }
194
195
        // Image style and class
196 3
        $image_style = '';
197 3
        $image_style .= 'width:'.$columnGroup->columns_images_width.'; ';
198 3
        if ($columnGroup->columns_round_images) {
199
            $image_style .= 'border-radius: 50%;';
200
        }
201
202 3
        $image_class = '';
203 3
        if ($columnGroup->columns_images_hide_mobile) {
204
            $image_class .= 'hide-image-mobile';
205
        }
206
207
        $ret = [
208 3
        'container_style' => $container_style,
209 3
        'icon_color' => 'color: '.$columnGroup->icon_color.';',
0 ignored issues
show
Bug introduced by
The property icon_color does not seem to exist on DavideCasiraghi\LaravelColumns\Models\ColumnGroup. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
210 3
        'group_title_style' => $group_title_style,
211 3
        'group_description_style' => $group_description_style,
212 3
        'group_button_style' => $group_button_style,
213 3
        'wrapper_style' => $wrapper_style,
214 3
        'title_style' => $title_style,
215 3
        'description_style'  => $description_style,
216 3
        'image_style'  => $image_style,
217 3
        'image_class'  => $image_class,
218 3
        'button_class' => $button_class,
219
    ];
220
221
        /*$ret = [
222
             'img_col_size_class' => 'col-md-'.$column->img_col_size,
223
             'text_col_size_class' => 'col-md-'.(12 - $column->img_col_size),
224
             'bkg_color' => 'background-color: '.$column->bkg_color.';',
225
             'text_color' => 'color: '.$column->text_color.';',
226
             'container_wrap' => ($column->container_wrap == 'true') ? 1 : 0,
227
         ];*/
228
229
        /*switch ($column->img_alignment) {
230
             case 'left':
231
                 $ret['img_col_order_class'] = 'order-md-1';
232
                 $ret['text_col_order_class'] = 'order-md-2';
233
                 break;
234
             case 'right':
235
                 $ret['img_col_order_class'] = 'order-md-2';
236
                 $ret['text_col_order_class'] = 'order-md-1';
237
                 break;
238
         }*/
239
240 3
        return $ret;
241
    }
242
}
243