Completed
Push — master ( 858c58...3e58c4 )
by Davide
09:34 queued 04:48
created

LaravelJumbotronImages::getParametersArray()   B

Complexity

Conditions 9
Paths 80

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 80
nop 1
dl 0
loc 31
ccs 21
cts 21
cp 1
crap 9
rs 8.0555
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 1
                    $ret['text_width'] = 'width: '.$jumbotronImage->text_width.'%;';
69 1
                break;
70 4
                case 'center': // Center
71 2
                    $ret['text_width'] = 'width: '.$jumbotronImage->text_width.'%; margin: auto;';
72 2
                break;
73 3
                case 'right': // Right
74 1
                    $ret['text_width'] = 'width: '.$jumbotronImage->text_width.'%; float: right;';
75 1
                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
146 2
        return $ret;
147
    }
148
149
    /**************************************************************************/
150
151
    /**
152
     *  Returns the snippet parameters.
153
     *
154
     *  @param array $matches
155
     *  @return array $ret
156
     **/
157 2
    public static function getSnippetParameters($matches)
158
    {
159 2
        $ret = [];
160
161
        // Get activation string parameters (from article)
162 2
        $ret['token'] = $matches[0];
163
        //dump($matches);
164 2
        $ret['jumbotron_id'] = $matches[2];
165
166
        //dump($ret);
167 2
        return $ret;
168
    }
169
170
}
171