Completed
Push — master ( 05ded5...0674f6 )
by Davide
05:57 queued 16s
created

LaravelCards::getSnippetParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 11
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DavideCasiraghi\LaravelCards;
4
5
class LaravelCards
6
{
7
    /*protected $cardModelConfig = [];
8
9
    public function __construct()
10
    {
11
        $this->cardModelConfig = config('laravel-cards.models.card');
12
    }*/
13
14
    /**************************************************************************/
15
16
    /**
17
     *  Provide the card data array (card_title, card_body, card_image).
18
     *
19
     *  @param int $cardId
20
     *  @return  \DavideCasiraghi\LaravelCards\Models\Card    $ret
21
     **/
22 3
    public static function getCard($cardId)
23
    {
24
        //$cardModel = $this->cardModelConfig['class'];
25 3
        $cardModel = config('laravel-cards.models.card.class');
26 3
        $ret = $cardModel::where('id', $cardId)->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+(card_id)=\[([^]]*)]
44
                \h*\#}/x';
45
46
        if (preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0)) {
47
            return $matches;
48
        } else {
49
            return;
50
        }
51 5
    }
52 4
53
    /**************************************************************************/
54 1
55
    /**
56
     *  Returns the plugin parameters.
57
     *
58
     *  @param array $matches
59
     *  @return array $ret
60
     **/
61
    public static function getSnippetParameters($matches)
62
    {
63
        $ret = [];
64
65
        // Get activation string parameters (from article)
66 3
        $ret['token'] = $matches[0];
67
        //dump($matches);
68 3
69
        $ret['card_id'] = $matches[2];
70
71 3
        return $ret;
72
    }
73
74 3
    /**************************************************************************/
75 3
76 3
    /**
77 3
     *  Return the same text with the cards HTML replaced
78 3
     *  where the token strings has been found.
79 3
     *
80 3
     *  @param string $text
81 3
     *  @return string $ret
82 3
     **/
83 3
    public function replace_card_snippets_with_template($text)
84
    {
85
        $matches = self::getCardSnippetOccurrences($text);
86
87
        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...
88 3
            foreach ($matches as $key => $single_gallery_matches) {
89
                $snippetParameters = self::getSnippetParameters($single_gallery_matches);
90 3
                $card = self::getCard($snippetParameters['card_id']);
91 3
                $cardParameters = ($card) ? $this->getParametersArray($card) : null;
92 2
93 2
                $cardView = self::showCard($card, $cardParameters);
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

93
                /** @scrutinizer ignore-call */ 
94
                $cardView = self::showCard($card, $cardParameters);
Loading history...
94 2
                $cardHtml = $cardView->render();
95 2
96 2
                // Substitute the card html to the token that has been found
97 2
                $text = str_replace($parameters['token'], $cardHtml, $text);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parameters seems to be never defined.
Loading history...
98 2
            }
99
        }
100
101
        $ret = $text;
102
103 3
        return $ret;
104
    }
105
106
    /***************************************************************************/
107
108
    /**
109
     * Show a Card.
110
     *
111
     * @param  \DavideCasiraghi\LaravelCards\Models\Card $card
112
     * @return \Illuminate\Http\Response
113
     */
114
    public function showCard($card, $cardParameters)
115
    {
116
        return view('laravel-cards::show-card', compact('card'))
117
            ->with('cardParameters', $cardParameters);
118
    }
119
    
120
    /***************************************************************************/
121
122
    /**
123
     * Return an array with the parameters for the show-card.
124
     * @param  \DavideCasiraghi\LaravelJumbotronImages\Models\Card  $card
0 ignored issues
show
Bug introduced by
The type DavideCasiraghi\LaravelJumbotronImages\Models\Card 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...
125
     * @return array
126
     */
127
    public static function getParametersArray($card)
128
    {
129
        $ret = [
130
             'img_col_size_class' => 'col-md-'.$card->img_col_size,
131
             'text_col_size_class' => 'col-md-'.(12-$card->img_col_size),
132
             'bkg_color' => 'background-color: '.$card->bkg_color.';',
133
             'text_color' => 'color: '.$card->text_color.';',
134
             'container_wrap' => ($card->container_wrap == 'true') ? 1 : 0,
135
         ];
136
         
137
        switch ($card->img_alignment) {
138
             case 'left':
139
                 $ret['img_col_order_class'] = 'order-md-1';
140
                 $ret['text_col_order_class'] = 'order-md-2';
141
                 break;
142
             case 'right':
143
                 $ret['img_col_order_class'] = 'order-md-2';
144
                 $ret['text_col_order_class'] = 'order-md-1';
145
                 break;
146
         }
147
        
148
        return $ret;
149
    }
150
151
}
152