Completed
Push — master ( 0f3f6d...5694f4 )
by ARCANEDEV
14s
created

RouteTracker::isTrackable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php namespace Arcanedev\LaravelTracker\Trackers;
2
3
use Arcanedev\LaravelTracker\Contracts\Trackers\RouteTracker as RouteTrackerContract;
4
use Arcanedev\LaravelTracker\Models;
5
use Arcanedev\LaravelTracker\Support\BindingManager;
6
use Illuminate\Database\Eloquent\Model as EloquentModel;
7
use Illuminate\Http\Request;
8
use Illuminate\Routing\Route;
9
use Illuminate\Support\Str;
10
11
/**
12
 * Class     RouteTracker
13
 *
14
 * @package  Arcanedev\LaravelTracker\Trackers
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class RouteTracker extends AbstractTracker implements RouteTrackerContract
18
{
19
    /* -----------------------------------------------------------------
20
     |  Getters and Setters
21
     | -----------------------------------------------------------------
22
     */
23
24
    /**
25
     * Get the model.
26
     *
27
     * @return \Arcanedev\LaravelTracker\Models\Route
28
     */
29 6
    protected function getModel()
30
    {
31 6
        return $this->makeModel(BindingManager::MODEL_ROUTE);
32
    }
33
34
    /* -----------------------------------------------------------------
35
     |  Main Methods
36
     | -----------------------------------------------------------------
37
     */
38
39
    /**
40
     * Check if the route is trackable.
41
     *
42
     * @param  \Illuminate\Routing\Route $route
43
     *
44
     * @return bool
45
     */
46 9
    public function isTrackable($route)
47
    {
48 9
        return ! $this->isInIgnoredRouteNames($route) &&
49 9
               ! $this->isInIgnoredUris($route);
50
    }
51
52
    /**
53
     * Track the matched route.
54
     *
55
     * @param  \Illuminate\Routing\Route  $route
56
     * @param  \Illuminate\Http\Request   $request
57
     *
58
     * @return int
59
     */
60 6
    public function track(Route $route, Request $request)
61
    {
62 6
        return $this->trackRoutePath(
63 6
            $route, $request, $this->trackRoute($route)
64 2
        );
65
    }
66
67
    /* -----------------------------------------------------------------
68
     |  Other Methods
69
     | -----------------------------------------------------------------
70
     */
71
72
    /**
73
     * Track the current route.
74
     *
75
     * @param  \Illuminate\Routing\Route  $route
76
     *
77
     * @return int
78
     */
79 6
    private function trackRoute(Route $route)
80
    {
81 6
        return $this->getModel()->newQuery()->firstOrCreate([
82 6
            'name'   => $this->getRouteName($route),
83 6
            'action' => $route->getActionName(),
84 6
        ])->getKey();
85
    }
86
87
    /**
88
     * Check if the route is ignored by a route pattern.
89
     *
90
     * @param  \Illuminate\Routing\Route  $route
91
     *
92
     * @return bool
93
     */
94 9
    private function isInIgnoredRouteNames($route)
95
    {
96 9
        return $this->checkPatterns(
97 9
            $route->getName(), $this->getConfig('routes.ignore.names', [])
98 3
        );
99
    }
100
101
    /**
102
     * Check if the route is ignored by a route pattern.
103
     *
104
     * @param  \Illuminate\Routing\Route  $route
105
     *
106
     * @return bool
107
     */
108 9
    private function isInIgnoredUris($route)
109
    {
110 9
        return $this->checkPatterns(
111 9
            $route->uri(), $this->getConfig('routes.ignore.uris', [])
112 3
        );
113
    }
114
115
    /**
116
     * Check if the value match the given patterns.
117
     *
118
     * @param  string|null  $value
119
     * @param  array        $patterns
120
     *
121
     * @return bool
122
     */
123 9
    private function checkPatterns($value, array $patterns)
124
    {
125 9
        foreach ($patterns as $pattern) {
126 9
            if (Str::is($pattern, $value)) return true;
127 3
        }
128
129 9
        return false;
130
    }
131
132
    /**
133
     * Get the route name.
134
     *
135
     * @param  \Illuminate\Routing\Route  $route
136
     *
137
     * @return string
138
     */
139 6
    private function getRouteName(Route $route)
140
    {
141 6
        return is_null($name = $route->getName()) ? $route->uri() : $name;
142
    }
143
144
    /**
145
     * Track the route path.
146
     *
147
     * @param  \Illuminate\Routing\Route  $route
148
     * @param  \Illuminate\Http\Request   $request
149
     * @param  int                        $routeId
150
     *
151
     * @return int
152
     */
153 6
    private function trackRoutePath(Route $route, Request $request, $routeId)
154
    {
155
        /** @var  \Arcanedev\LaravelTracker\Models\RoutePath  $model */
156 6
        $model = $this->makeModel(BindingManager::MODEL_ROUTE_PATH)->firstOrCreate([
157 6
            'route_id' => $routeId,
158 6
            'path'     => $request->path(),
159 2
        ]);
160
161 6
        if ($model->wasRecentlyCreated)
162 6
            $this->trackRoutePathParameters($route, $model);
163
164 6
        return $model->getKey();
165
    }
166
167
    /**
168
     * Track the route path parameters.
169
     *
170
     * @param  \Illuminate\Routing\Route                   $route
171
     * @param  \Arcanedev\LaravelTracker\Models\RoutePath  $routePath
172
     */
173 6
    private function trackRoutePathParameters(Route $route, Models\RoutePath $routePath)
174
    {
175 6
        $parameters = [];
176
177 6
        foreach ($route->parameters() as $parameter => $value) {
178 3
            $parameters[] = $this->makeModel(BindingManager::MODEL_ROUTE_PATH_PARAMETER)->fill([
179 3
                'parameter' => $parameter,
180 3
                'value'     => $this->checkIfValueIsEloquentModel($value),
181 1
            ]);
182 2
        }
183
184 6
        if (count($parameters) > 0)
185 4
            $routePath->parameters()->saveMany($parameters);
186 6
    }
187
188
    /**
189
     * Check if the value is an eloquent model.
190
     *
191
     * @param  mixed  $value
192
     *
193
     * @return mixed
194
     */
195 3
    private function checkIfValueIsEloquentModel($value)
196
    {
197 3
        if ($value instanceof EloquentModel) {
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...
198
            foreach ($this->getConfig('routes.model-columns', ['id']) as $column) {
199
                if (
200
                    array_key_exists($column, $attributes = $value->getAttributes()) &&
201
                    ! is_null($attributes[$column])
202
                ) {
203
                    return $attributes[$column];
204
                }
205
            }
206
        }
207
208 3
        return $value;
209
    }
210
}
211