LaravelCards::showCard()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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

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