Completed
Push — master ( 70421f...8ae839 )
by ARCANEDEV
08:37
created

RouteTracker::checkIfValueIsEloquentModel()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
ccs 0
cts 14
cp 0
cc 5
eloc 8
nc 3
nop 1
crap 30
1
<?php namespace Arcanedev\LaravelTracker\Trackers;
2
3
use Arcanedev\LaravelTracker\Contracts\Trackers\RouteTracker as RouteTrackerContract;
4
use Illuminate\Http\Request;
5
use Illuminate\Routing\Route;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
use Arcanedev\LaravelTracker\Models;
9
10
/**
11
 * Class     RouteTracker
12
 *
13
 * @package  Arcanedev\LaravelTracker\Trackers
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class RouteTracker implements RouteTrackerContract
17
{
18
    /* ------------------------------------------------------------------------------------------------
19
     |  Main Functions
20
     | ------------------------------------------------------------------------------------------------
21
     */
22
    /**
23
     * Check if the route is trackable.
24
     *
25
     * @param  \Illuminate\Routing\Route $route
26
     *
27
     * @return bool
28
     */
29 4
    public function isTrackable($route)
30
    {
31 4
        if ($this->isInIgnoredRouteNames($route)) return false;
32
33 4
        return true;
34
    }
35
36
    /**
37
     * Track the matched route.
38
     *
39
     * @param  \Illuminate\Routing\Route  $route
40
     * @param  \Illuminate\Http\Request   $request
41
     *
42
     * @return int
43
     */
44 2
    public function track(Route $route, Request $request)
45
    {
46 2
        return $this->trackRoutePath(
47 1
            $route,
48 1
            $request,
49 2
            $this->trackRoute($route)
50 1
        );
51
    }
52
53
    /* ------------------------------------------------------------------------------------------------
54
     |  Other Functions
55
     | ------------------------------------------------------------------------------------------------
56
     */
57
    /**
58
     * Track the current route.
59
     *
60
     * @param  \Illuminate\Routing\Route  $route
61
     *
62
     * @return int
63
     */
64 2
    private function trackRoute(Route $route)
65
    {
66 2
        $model = Models\Route::firstOrCreate([
67 2
            'name'   => $this->getRouteName($route),
68 2
            'action' => $route->getActionName(),
69 1
        ]);
70
71 2
        return $model->id;
72
    }
73
74
    /**
75
     * Check if the route is ignored by a route pattern.
76
     *
77
     * @param  \Illuminate\Routing\Route  $route
78
     *
79
     * @return bool
80
     */
81 4
    private function isInIgnoredRouteNames($route)
82
    {
83
        if (
84 4
            ! is_null($name  = $route->getName()) &&
85 4
            count($names = config('laravel-tracker.routes.ignore.names', [])) > 0
86 2
        ) {
87 4
            foreach ($names as $pattern) {
88 4
                if (Str::is($pattern, $name)) return true;
89 2
            }
90 2
        }
91
92 4
        return false;
93
    }
94
95
    /**
96
     * Get the route name.
97
     *
98
     * @param  \Illuminate\Routing\Route  $route
99
     *
100
     * @return string
101
     */
102 2
    private function getRouteName(Route $route)
103
    {
104 2
        if ($name = $route->getName())
105 2
            return $name;
106
107
        if ($name = Arr::get($route->getAction(), 'as'))
108
            return $name;
109
110
        return $route->getUri();
111
    }
112
113
    /**
114
     * Track the route path.
115
     *
116
     * @param  \Illuminate\Routing\Route  $route
117
     * @param  \Illuminate\Http\Request   $request
118
     * @param  int                        $routeId
119
     *
120
     * @return int
121
     */
122 2
    private function trackRoutePath(Route $route, Request $request, $routeId)
123
    {
124
        /** @var  \Arcanedev\LaravelTracker\Models\RoutePath  $model */
125 2
        $model = Models\RoutePath::firstOrCreate([
126 2
            'route_id' => $routeId,
127 2
            'path'     => $request->path(),
128 1
        ]);
129
130 2
        if ($model->wasRecentlyCreated)
131 2
            $this->trackRoutePathParameters($route, $model);
132
133 2
        return $model->id;
134
    }
135
136
    /**
137
     * Track the route path parameters.
138
     *
139
     * @param  \Illuminate\Routing\Route                   $route
140
     * @param  \Arcanedev\LaravelTracker\Models\RoutePath  $routePath
141
     */
142 2
    private function trackRoutePathParameters(Route $route, Models\RoutePath $routePath)
143
    {
144 2
        $parameters = [];
145
146 2
        if (count($params = $route->parameters()) > 0) {
147
            foreach ($params as $parameter => $value) {
148
                $parameters[] = new Models\RoutePathParameter([
149
                    'parameter' => $parameter,
150
                    'value'     => $this->checkIfValueIsEloquentModel($value),
151
                ]);
152
            }
153
154
            $routePath->parameters()->saveMany($parameters);
155
        }
156 2
    }
157
158
    /**
159
     * Check if the value is an eloquent model.
160
     *
161
     * @param  mixed  $value
162
     *
163
     * @return mixed
164
     */
165
    private function checkIfValueIsEloquentModel($value)
166
    {
167
        if ($value instanceof \Illuminate\Database\Eloquent\Model) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Database\Eloquent\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
168
            foreach (config('laravel-tracker.routes.model-columns', ['id']) as $column) {
169
                if (
170
                    array_key_exists($column, $attributes = $value->getAttributes()) &&
171
                    ! is_null($attributes[$column])
172
                ) {
173
                    return $attributes[$column];
174
                }
175
            }
176
        }
177
178
        return $value;
179
    }
180
}
181