Completed
Push — master ( 31f349...1829f7 )
by ARCANEDEV
11:26
created

RouteTracker::isInIgnoredUris()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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
        if ( ! is_null($value) && count($patterns) > 0) {
126 18
            foreach ($patterns as $pattern) {
127 18
                if (Str::is($pattern, $value)) return true;
128 9
            }
129 9
        }
130
131 18
        return false;
132
    }
133
134
    /**
135
     * Get the route name.
136
     *
137
     * @param  \Illuminate\Routing\Route  $route
138
     *
139
     * @return string
140
     */
141 12
    private function getRouteName(Route $route)
142
    {
143 12
        if ($name = $route->getName())
144 12
            return $name;
145
146
        if ($name = Arr::get($route->getAction(), 'as'))
147
            return $name;
148
149
        return $route->getUri();
150
    }
151
152
    /**
153
     * Track the route path.
154
     *
155
     * @param  \Illuminate\Routing\Route  $route
156
     * @param  \Illuminate\Http\Request   $request
157
     * @param  int                        $routeId
158
     *
159
     * @return int
160
     */
161 12
    private function trackRoutePath(Route $route, Request $request, $routeId)
162
    {
163
        /** @var  \Arcanedev\LaravelTracker\Models\RoutePath  $model */
164 12
        $model = $this->makeModel(AbstractModel::MODEL_ROUTE_PATH)->firstOrCreate([
165 12
            'route_id' => $routeId,
166 12
            'path'     => $request->path(),
167 6
        ]);
168
169 12
        if ($model->wasRecentlyCreated)
170 12
            $this->trackRoutePathParameters($route, $model);
171
172 12
        return $model->id;
173
    }
174
175
    /**
176
     * Track the route path parameters.
177
     *
178
     * @param  \Illuminate\Routing\Route                   $route
179
     * @param  \Arcanedev\LaravelTracker\Models\RoutePath  $routePath
180
     */
181 12
    private function trackRoutePathParameters(Route $route, Models\RoutePath $routePath)
182
    {
183 12
        $parameters = [];
184
185 12
        if (count($params = $route->parameters()) > 0) {
186 6
            foreach ($params as $parameter => $value) {
187 6
                $parameters[] = $this->makeModel(AbstractModel::MODEL_ROUTE_PATH_PARAMETER)->fill([
188 6
                    'parameter' => $parameter,
189 6
                    'value'     => $this->checkIfValueIsEloquentModel($value),
190 3
                ]);
191 3
            }
192
193 6
            $routePath->parameters()->saveMany($parameters);
194 3
        }
195 12
    }
196
197
    /**
198
     * Check if the value is an eloquent model.
199
     *
200
     * @param  mixed  $value
201
     *
202
     * @return mixed
203
     */
204 6
    private function checkIfValueIsEloquentModel($value)
205
    {
206 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...
207
            foreach ($this->getConfig('routes.model-columns', ['id']) as $column) {
208
                if (
209
                    array_key_exists($column, $attributes = $value->getAttributes()) &&
210
                    ! is_null($attributes[$column])
211
                ) {
212
                    return $attributes[$column];
213
                }
214
            }
215
        }
216
217 6
        return $value;
218
    }
219
}
220