Completed
Push — master ( 3e9a56...e6b14e )
by Davide
06:59 queued 10s
created

getJumbotronSnippetOccurrences()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DavideCasiraghi\LaravelJumbotronImages;
4
5
use DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage;
6
7
class LaravelJumbotronImages
8
{
9
    /**
10
     * Return a Jumbotron image.
11
     *
12
     * @param  int  $jumbotronImageId
13
     * @return \DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage
14
     */
15 1
    public function getJumbotronImage($jumbotronImageId)
16
    {
17 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
18 1
        $jumbotronImage->parameters = $this->getParametersArray($jumbotronImage);
19
20 1
        $ret = $jumbotronImage;
21
22 1
        return $ret;
23
    }
24
25
    /***************************************************************************/
26
27
    /**
28
     * Show a Jumbotron Image.
29
     *
30
     * @param  int  $jumbotronImageId
31
     * @return \DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage
32
     */
33 2
    public function showJumbotronImage($jumbotronImageId)
34
    {
35 2
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
36 2
        $jumbotronImageParameters = $this->getParametersArray($jumbotronImage);
37
        
38 2
        return view('laravel-jumbotron-images::show-jumbotron-image', compact('jumbotronImage'))
39 2
            ->with('jumbotronImageParameters', $jumbotronImageParameters);
40
    }
41
42
    /***************************************************************************/
43
44
    /**
45
     * Attach to the jumbotron image object an array with the parameters for the show-jumbotron-image view.
46
     * @param  \DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage  $jumbotronImage
47
     * @return array
48
     */
49 4
    public static function getParametersArray($jumbotronImage)
50
    {
51
        $ret = [
52 4
             'cover_opacity' => 'opacity: '.$jumbotronImage->cover_opacity.';',
53 4
             'background_color' => 'background: #'.$jumbotronImage->background_color.';',
54 4
             'image' => 'background-image:url(/storage/images/jumbotron_images/'.$jumbotronImage->image_file_name.');',
55 4
             'text_horizontal_alignment' => 'text-align: '.$jumbotronImage->text_horizontal_alignment.';',
56
         ];
57 4
        $ret['white_moon'] = ($jumbotronImage->white_moon == 1) ? ' moon-curve ' : '';
58 4
        $ret['scroll_down_arrow'] = ($jumbotronImage->scroll_down_arrow == 1) ? "<div class='scroll-arrow white'><span>SCROLL DOWN</span><img src='/vendor/laravel-jumbotron-images/assets/images/angle-down-regular.svg'></div>" : '';
59
60
        /* Parallax - The element is defined with stellar plugin like: <section class="parallax" data-stellar-background-ratio="0.5" ><span>Summer</span></section>*/
61 4
        $ret['parallax'] = ($jumbotronImage->parallax == 1) ? ' parallax' : '';
62 4
        $ret['parallax_ratio'] = ($jumbotronImage->parallax == 1) ? "data-stellar-background-ratio='0.5'" : '';
63
64
        /* Text Width */
65 4
        if ($jumbotronImage->text_width != 100) {
66 4
            switch ($jumbotronImage->text_horizontal_alignment) {
67 4
                case 'left':	// Left
68
                    $ret['text_width'] = 'width: '.$jumbotronImage->text_width.'%;';
69
                break;
70 4
                case 'center': // Center
71 2
                    $ret['text_width'] = 'width: '.$jumbotronImage->text_width.'%; margin: auto;';
72 2
                break;
73 2
                case 'right': // Right
74
                    $ret['text_width'] = 'width: '.$jumbotronImage->text_width.'%; float: right;';
75
                break;
76
            }
77
        }
78
79 4
        return $ret;
80
    }
81
    
82
    /**************************************************************************/
83
84
    /**
85
     *  Find the card snippet occurances in the text.
86
     *
87
     *  @param string $text
88
     *  @return array $matches
89
     **/
90 2
    public static function getJumbotronSnippetOccurrences($text)
91
    {
92 2
        $re = '/{\#
93
                \h+jumbotron
94
                \h+(id)=\[([^]]*)]
95
                \h*\#}/x';
96
97 2
        if (preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0)) {
98 2
            return $matches;
99
        } else {
100 1
            return;
101
        }
102
    }
103
    
104
    /**************************************************************************/
105
106
    /**
107
     *  Return the same text with the jumbotrons HTML replaced
108
     *  where the token strings has been found.
109
     *
110
     *  @param string $text
111
     *  @return string $ret
112
     **/
113 1
    public function replaceJumbotronSnippetsWithTemplate($text)
114
    {
115 1
        $matches = self::getJumbotronSnippetOccurrences($text);
116
117 1
        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...
118 1
            foreach ($matches as $key => $single_jumbotron_matches) {
119 1
                $snippetParameters = self::getSnippetParameters($single_jumbotron_matches);
120 1
                $jumbotron = self::getJumbotron($snippetParameters['jumbotron_id']);
0 ignored issues
show
Unused Code introduced by
The assignment to $jumbotron is dead and can be removed.
Loading history...
121 1
                $jumbotronView = self::showJumbotronImage($snippetParameters['jumbotron_id']);
0 ignored issues
show
Bug Best Practice introduced by
The method DavideCasiraghi\LaravelJ...s::showJumbotronImage() 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

121
                /** @scrutinizer ignore-call */ 
122
                $jumbotronView = self::showJumbotronImage($snippetParameters['jumbotron_id']);
Loading history...
122 1
                $jumbotronHtml = $jumbotronView->render(); 
123
                
124
                // Substitute the jumbotron html to the token that has been found
125 1
                $text = str_replace($snippetParameters['token'], $jumbotronHtml, $text);
0 ignored issues
show
Bug introduced by
It seems like $jumbotronHtml can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $replace of str_replace() does only seem to accept string|string[], maybe add an additional type check? ( Ignorable by Annotation )

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

125
                $text = str_replace($snippetParameters['token'], /** @scrutinizer ignore-type */ $jumbotronHtml, $text);
Loading history...
126
            }
127
        }
128
129 1
        $ret = $text;
130
131 1
        return $ret;
132
    }
133
    
134
    /**************************************************************************/
135
136
    /**
137
     *  Provide the post data array (post_title, post_body, post_image).
138
     *
139
     *  @param int $jumbotronId
140
     *  @return  \DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage    $ret
141
     **/
142 2
    public static function getJumbotron($jumbotronId)
143
    {
144 2
        $ret = JumbotronImage::where('id', $jumbotronId)->first();
145 2
        return $ret;
146
    }
147
    
148
    /**************************************************************************/
149
150
    /**
151
     *  Returns the snippet parameters.
152
     *
153
     *  @param array $matches
154
     *  @return array $ret
155
     **/
156 2
    public static function getSnippetParameters($matches)
157
    {
158 2
        $ret = [];
159
160
        // Get activation string parameters (from article)
161 2
        $ret['token'] = $matches[0];
162
        //dump($matches);
163 2
        $ret['jumbotron_id'] = $matches[2];
164
        
165
        //dump($ret);
166 2
        return $ret;
167
    }
168
    
169
    /**
170
     *  Prepare the card HTML.
171
     *
172
     *  @param array $parameters
173
     *  @param \DavideCasiraghi\LaravelCards\Models\Post $post
0 ignored issues
show
Bug introduced by
The type DavideCasiraghi\LaravelCards\Models\Post was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
174
     *
175
     *  @return string $ret
176
     **/
177
    public static function prepareJumbotronHtml($parameters, $jumbotron)
0 ignored issues
show
Unused Code introduced by
The parameter $jumbotron is not used and could be removed. ( Ignorable by Annotation )

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

177
    public static function prepareJumbotronHtml($parameters, /** @scrutinizer ignore-unused */ $jumbotron)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
178
    {
179
        if (! is_null($post)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $post seems to be never defined.
Loading history...
180
            $ret = "<div class='row featurette' style='".$parameters['bkg_color'].' '.$parameters['text_color']."'>";
181
            if ($parameters['container_wrap']) {
182
                $ret .= "<div class='container'>";
183
            }
184
            $ret .= "<div class='text ".$parameters['text_col_size_class'].' my-auto px-4 '.$parameters['text_col_order_class']."'>";
185
            $ret .= "<h2 class='featurette-heading mt-5'>".$post['title'].'</h2>';
186
            $ret .= "<div class='lead mb-4'>".$post['body'].'</div>';
187
            $ret .= '</div>';
188
            $ret .= "<div class='image ".$parameters['img_col_size_class'].' '.$parameters['img_col_order_class']."'>";
189
            if (! empty($post['post_image_src'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $post seems to never exist and therefore empty should always be true.
Loading history...
190
                $ret .= "<img class='featurette-image img-fluid mx-auto' src='".$post['image']."' alt='".$post['image_alt']."'>";
191
            }
192
            $ret .= '</div>';
193
            if ($parameters['container_wrap']) {
194
                $ret .= '</div>';
195
            }
196
            $ret .= '</div>';
197
        } else {
198
            $ret = "<div class='alert alert-warning' role='alert'>The post with id ".$parameters['post_id'].' has not been found.</div>';
199
        }
200
201
        return $ret;
202
    }
203
}
204