Completed
Push — master ( 1829f7...f36413 )
by ARCANEDEV
08:45
created

RouteTracker::trackRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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