Completed
Push — master ( 3baa34...942b42 )
by Davide
04:15 queued 39s
created

LaravelCards::getParameters()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 23
nc 6
nop 1
dl 0
loc 38
ccs 24
cts 24
cp 1
crap 4
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace DavideCasiraghi\LaravelCards;
4
5
class LaravelCards
6
{
7
    /*protected $postModelConfig = [];
8
9
    public function __construct()
10
    {
11
        $this->postModelConfig = config('laravel-cards.models.post');
12
    }*/
13
14
    /**************************************************************************/
15
16
    /**
17
     *  Provide the post data array (post_title, post_body, post_image).
18
     *
19
     *  @param int $postId
20
     *  @return  \DavideCasiraghi\LaravelCards\Models\Post    $ret
21
     **/
22 3
    public static function getPost($postId)
23
    {
24
        //$postModel = $this->postModelConfig['class'];
25 3
        $postModel = config('laravel-cards.models.post.class');
26 3
        $ret = $postModel::where('id', $postId)->first();
27
28 3
        return $ret;
29
    }
30
31
    /**************************************************************************/
32
33
    /**
34
     *  Find the card snippet occurances in the text.
35
     *
36
     *  @param string $text
37
     *  @return array $matches
38
     **/
39 5
    public static function getCardSnippetOccurrences($text)
40
    {
41 5
        $re = '/{\#
42
                \h+card
43
                \h+(post_id|img_alignment|img_col_size|bkg_color|text_color|container_wrap)=\[([^]]*)]
44
                \h+((?1))=\[([^]]*)]
45
                \h+((?1))=\[([^]]*)] 
46
                \h+((?1))=\[([^]]*)]
47
                \h+((?1))=\[([^]]*)] 
48
                \h+((?1))=\[([^]]*)] 
49
                \h*\#}/x';
50
51 5
        if (preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0)) {
52 4
            return $matches;
53
        } else {
54 1
            return;
55
        }
56
    }
57
58
    /**************************************************************************/
59
60
    /**
61
     *  Returns the plugin parameters.
62
     *
63
     *  @param array $matches
64
     *  @return array $ret
65
     **/
66 3
    public static function getParameters($matches)
67
    {
68 3
        $ret = [];
69
70
        // Get activation string parameters (from article)
71 3
        $ret['token'] = $matches[0];
72
        //dump($matches);
73
74 3
        $ret['post_id'] = $matches[2];
75 3
        $ret['img_col_size_class'] = 'col-md-'.$matches[6];
76 3
        $textColSize = 12 - $matches[6];
77 3
        $ret['text_col_size_class'] = 'col-md-'.$textColSize;
78 3
        $backgroundColor = $matches[8];
79 3
        $ret['bkg_color'] = 'background-color: '.$backgroundColor.';';
80 3
        $textColor = $matches[10];
81 3
        $ret['text_color'] = 'color: '.$textColor.';';
82 3
        $containerWrap = $matches[12];
83 3
        $ret['container_wrap'] = ($containerWrap == 'true') ? 1 : 0;
84
85
        //dd($ret['bkg_color']);
86
        // Image alignment
87
        //$ret['img_alignment'] = $matches[4];
88 3
        $imageAlignment = $matches[4];
89
90 3
        switch ($imageAlignment) {
91 3
             case 'left':
92 2
                 $ret['img_col_order_class'] = 'order-md-1';
93 2
                 $ret['text_col_order_class'] = 'order-md-2';
94 2
                 break;
95 2
             case 'right':
96 2
                 $ret['img_col_order_class'] = 'order-md-2';
97 2
                 $ret['text_col_order_class'] = 'order-md-1';
98 2
                 break;
99
         }
100
101
        //dump($ret);
102
103 3
        return $ret;
104
    }
105
106
    /**************************************************************************/
107
108
    /**
109
     *  Prepare the card HTML.
110
     *
111
     *  @param array $parameters
112
     *  @param \DavideCasiraghi\LaravelCards\Models\Post $post
113
     *
114
     *  @return string $ret
115
     **/
116
    /*public static function prepareCardHtml($parameters, $post)
117
    {
118
        if (! is_null($post)) {
119
            $ret = "<div class='row featurette' style='".$parameters['bkg_color'].' '.$parameters['text_color']."'>";
120
            if ($parameters['container_wrap']) {
121
                $ret .= "<div class='container'>";
122
            }
123
            $ret .= "<div class='text ".$parameters['text_col_size_class'].' my-auto px-4 '.$parameters['text_col_order_class']."'>";
124
            $ret .= "<h2 class='featurette-heading mt-5'>".$post['title'].'</h2>';
125
            $ret .= "<div class='lead mb-4'>".$post['body'].'</div>';
126
            $ret .= '</div>';
127
128
            if (! empty($post['introimage'])) {
129
                $ret .= "<div class='image d-none d-md-block ".$parameters['img_col_size_class'].' '.$parameters['img_col_order_class']."'
130
                        style='
131
                        background-size: cover; 
132
                        background-image: url(/storage/images/posts_intro_images/".$post['introimage'].");
133
                        min-height: 400px;
134
                        background-position: 50% 50%;
135
                        '>";
136
                $ret .= '</div>';
137
138
                $ret .= "<div class='image col-12 d-md-none ".$parameters['img_col_order_class']."'>";
139
                $ret .= "<img class='featurette-image img-fluid mx-auto' src='/storage/images/posts_intro_images/".$post['introimage']."' alt='".$post['introimage_alt']."'>";
140
                $ret .= '</div>';
141
            }
142
143
144
            if ($parameters['container_wrap']) {
145
                $ret .= '</div>';
146
            }
147
            $ret .= '</div>';
148
        } else {
149
            $ret = "<div class='alert alert-warning' role='alert'>The post with id ".$parameters['post_id'].' has not been found.</div>';
150
        }
151
152
        return $ret;
153
    }*/
154
155
    /**************************************************************************/
156
157
    /**
158
     *  Return the same text with the cards HTML replaced
159
     *  where the token strings has been found.
160
     *
161
     *  @param string $text
162
     *  @return string $ret
163
     **/
164 2
    public function replace_card_snippets_with_template($text)
165
    {
166 2
        $matches = self::getCardSnippetOccurrences($text);
167
168 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...
169 2
            foreach ($matches as $key => $single_gallery_matches) {
170 2
                $parameters = self::getParameters($single_gallery_matches);
171 2
                $post = self::getPost($parameters['post_id']);
172
                //$cardHtml = self::prepareCardHtml($parameters, $post);
173
174 2
                $cardView = self::showCard($post, $parameters);
0 ignored issues
show
Bug Best Practice introduced by
The method DavideCasiraghi\LaravelC...aravelCards::showCard() 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

174
                /** @scrutinizer ignore-call */ 
175
                $cardView = self::showCard($post, $parameters);
Loading history...
175 2
                $cardHtml = $cardView->render();
176
177
                // Substitute the card html to the token that has been found
178 2
                $text = str_replace($parameters['token'], $cardHtml, $text);
179
            }
180
        }
181
182 2
        $ret = $text;
183
184 2
        return $ret;
185
    }
186
187
    /***************************************************************************/
188
189
    /**
190
     * Show a Card.
191
     *
192
     * @param  \DavideCasiraghi\LaravelCards\Models\Post $post
193
     * @return \Illuminate\Http\Response
194
     */
195 2
    public function showCard($post, $parameters)
196
    {
197
        //$postParameters = ($post) ? $this->getParametersArray($post) : null;
198
199 2
        return view('laravel-cards::show-card', compact('post'))
200 2
            ->with('parameters', $parameters);
201
    }
202
}
203