Completed
Branch master (7a36f1)
by Mohamed
04:08
created

BladeServiceProvider::boot()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 62
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 4.0029

Importance

Changes 6
Bugs 5 Features 0
Metric Value
c 6
b 5
f 0
dl 0
loc 62
ccs 33
cts 35
cp 0.9429
rs 8.9167
cc 4
eloc 33
nc 1
nop 0
crap 4.0029

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Providers;
13
14
use Illuminate\Support\ServiceProvider;
15
16
/**
17
 * BladeServiceProvider is the blade service provider for extending blade template engine.
18
 *
19
 * @author Mohamed Alsharaf <[email protected]>
20
 */
21
class BladeServiceProvider extends ServiceProvider
22
{
23
    /**
24
     * @var array
25
     */
26
    protected $replacements = [
27
        'macro'    => "<?php \$___tiny['%s']=function(%s)use(\$__env){ ob_start(); ?>\n",
28
        'usemacro' => "<?php echo \$___tiny['%s'](%s); ?>\n",
29
    ];
30
31
    /**
32
     * Bootstrap any application services.
33
     */
34 63
    public function boot()
35
    {
36
        \Blade::directive('macro', function ($expression) {
37 2
            return $this->directive('macro', $expression);
38 63
        });
39
40 63
        \Blade::directive(
41 63
            'endmacro',
42
            function () {
43 2
                return "\n<?php return ob_get_clean();} ?>\n";
44 63
            }
45
        );
46
47
        \Blade::directive('usemacro', function ($expression) {
48 2
            return $this->directive('usemacro', $expression);
49 63
        });
50
51 63
        \Blade::directive(
52 63
            'permission',
53
            function ($expression) {
54 5
                return "<?php if(!Auth::guest() && Auth::user()->permission{$expression}): ?>";
55 63
            }
56
        );
57
58 63
        \Blade::directive(
59 63
            'endpermission',
60
            function () {
61 5
                return '<?php endif; ?>';
62 63
            }
63
        );
64
65 63
        \Blade::directive(
66 63
            'mailattrs',
67
            function ($expression) {
68
                // Get parameters
69 1
                $params = array_map('trim', explode('|', trim($expression, '()')));
70
                // Get style based on the first parameter
71 1
                $style = config('mailcss.' . $params[0]);
72
73 1
                if (!$style) {
74
                    return '';
75
                }
76
77
                // Convert the parameters starting from second into key => value array
78 1
                $override = array_reduce(array_slice($params, 1), function ($override, $param) {
79 1
                    $segments = array_map('trim', explode('=', $param));
80 1
                    $override[$segments[0]] = $segments[1];
81
82 1
                    return $override;
83 1
                }, []);
84
85
                // Style can be callback or an array. Merge and echo.
86 1
                if (is_callable($style)) {
87 1
                    $style = $style($override);
88 1
                } elseif (isset($params[1])) {
89
                    $style = array_merge($style, $override);
90
                }
91
92 1
                return "<?php echo '" . \Html::attributes($style) . "'; ?>";
93 63
            }
94
        );
95 63
    }
96
97
    /**
98
     * Callback method for macro directive.
99
     *
100
     * @param string $name
101
     * @param string $expression
102
     *
103
     * @return string
104
     */
105 2
    protected function directive($name, $expression)
106
    {
107 2
        $arguments = $this->extractArgumentsAndName($expression);
108
109 2
        return sprintf($this->replacements[$name], $arguments['name'], $arguments['args']);
110
    }
111
112
    /**
113
     * Return arguments and name from blade expression.
114
     *
115
     * @param $expression
116
     *
117
     * @return array
118
     */
119 2
    protected function extractArgumentsAndName($expression)
120
    {
121 2
        $pattern = '/(\([\'|\"](\w+)[\'|\"],\s*(([^\@])+|(.*))\))/xim';
122 2
        $matches = [];
123 2
        preg_match_all($pattern, $expression, $matches);
124
125 2
        if (!isset($matches[3][0])) {
126
            throw new \InvalidArgumentException(sprintf('Invalid arguments in blade: macro%s', $expression));
127
        }
128
129
        return [
130 2
            'name' => $matches[2][0],
131 2
            'args' => $matches[3][0],
132
        ];
133
    }
134
135
    /**
136
     * Register any application services.
137
     *
138
     * This service provider is a great spot to register your various container
139
     * bindings with the application. As you can see, we are registering our
140
     * "Registrar" implementation here. You can add your own bindings too!
141
     */
142 63
    public function register()
143
    {
144 63
    }
145
}
146