Controller   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 19
c 4
b 2
f 0
lcom 0
cbo 3
dl 0
loc 69
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C show() 0 66 19
1
<?php
2
3
namespace Gregoriohc\Preview;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Http\Request;
8
use Illuminate\Routing\Controller as BaseController;
9
10
class Controller extends BaseController
11
{
12
    public function show($view, Request $request)
13
    {
14
        if (!PreviewServiceProvider::isEnabled()) {
15
            abort(404);
16
        }
17
18
        $data = $request->all();
19
20
        array_walk($data, function (&$value) {
21
            switch ($value) {
22
                case is_object(json_decode($value)):
23
                    $value = json_decode($value);
24
                    break;
25
                case str_contains($value, '::'):
26
                    $parts = explode('::', $value);
27
28
                    switch (count($parts)) {
29
                        case 0:
30
                            break;
31
                        case 1:
32
                            break;
33
                        default:
34
                            if (2 == count($parts) && is_numeric($parts[1])) {
35
                                list($class, $id) = $parts;
36
                                if (class_exists($class)) {
37
                                    try {
38
                                        $object = new $class();
39
                                        if ($object instanceof Model && is_numeric($id)) {
40
                                            $value = call_user_func_array([$class, 'findOrFail'], [$id]);
41
                                        }
42
                                    } catch (Exception $e) { /* Ignore errors */
43
                                    }
44
                                }
45
                            } else {
46
                                list($class, $method) = array_slice($parts, 0, 2);
47
                                $params = array_slice($parts, 2);
48
                                if (class_exists($class)) {
49
                                    $updated = false;
50
                                    try {
51
                                        if (is_callable([$class, $method])) {
52
                                            $value = call_user_func_array([$class, $method], $params);
53
                                            $updated = true;
54
                                        }
55
                                    } catch (Exception $e) { /* Ignore errors */
56
                                    }
57
                                    if (!$updated) {
58
                                        try {
59
                                            if (is_callable([$class, $method])) {
60
                                                $object = new $class();
61
                                                if (is_callable([$object, $method])) {
62
                                                    $value = call_user_func_array([$object, $method], $params);
63
                                                }
64
                                            }
65
                                        } catch (Exception $e) { /* Ignore errors */
66
                                        }
67
                                    }
68
                                }
69
                            }
70
                            break;
71
                    }
72
                    break;
73
            }
74
        });
75
76
        return view($view, $data);
77
    }
78
}
79