Completed
Push — master ( 5910d3...b700fe )
by ARCANEDEV
15:35 queued 06:57
created

RouteTracker::getRouteName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
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\Models\AbstractModel;
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
     * 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(), $this->getConfig('routes.ignore.names', [])
96 9
        );
97
    }
98
99
    /**
100
     * Check if the route is ignored by a route pattern.
101
     *
102
     * @param  \Illuminate\Routing\Route  $route
103
     *
104
     * @return bool
105
     */
106 18
    private function isInIgnoredUris($route)
107
    {
108 18
        return $this->checkPatterns(
109 18
            $route->uri(), $this->getConfig('routes.ignore.uris', [])
110 9
        );
111
    }
112
113
    /**
114
     * Check if the value match the given patterns.
115
     *
116
     * @param  string|null  $value
117
     * @param  array        $patterns
118
     *
119
     * @return bool
120
     */
121 18
    private function checkPatterns($value, array $patterns)
122
    {
123 18
        foreach ($patterns as $pattern) {
124 18
            if (Str::is($pattern, $value)) return true;
125 9
        }
126
127 18
        return false;
128
    }
129
130
    /**
131
     * Get the route name.
132
     *
133
     * @param  \Illuminate\Routing\Route  $route
134
     *
135
     * @return string
136
     */
137 12
    private function getRouteName(Route $route)
138
    {
139 12
        return is_null($name = $route->getName()) ? $route->getUri() : $name;
140
    }
141
142
    /**
143
     * Track the route path.
144
     *
145
     * @param  \Illuminate\Routing\Route  $route
146
     * @param  \Illuminate\Http\Request   $request
147
     * @param  int                        $routeId
148
     *
149
     * @return int
150
     */
151 12
    private function trackRoutePath(Route $route, Request $request, $routeId)
152
    {
153
        /** @var  \Arcanedev\LaravelTracker\Models\RoutePath  $model */
154 12
        $model = $this->makeModel(AbstractModel::MODEL_ROUTE_PATH)->firstOrCreate([
155 12
            'route_id' => $routeId,
156 12
            'path'     => $request->path(),
157 6
        ]);
158
159 12
        if ($model->wasRecentlyCreated)
160 12
            $this->trackRoutePathParameters($route, $model);
161
162 12
        return $model->id;
163
    }
164
165
    /**
166
     * Track the route path parameters.
167
     *
168
     * @param  \Illuminate\Routing\Route                   $route
169
     * @param  \Arcanedev\LaravelTracker\Models\RoutePath  $routePath
170
     */
171 12
    private function trackRoutePathParameters(Route $route, Models\RoutePath $routePath)
172
    {
173 12
        $parameters = [];
174
175 12
        foreach ($route->parameters() as $parameter => $value) {
176 6
            $parameters[] = $this->makeModel(AbstractModel::MODEL_ROUTE_PATH_PARAMETER)->fill([
177 6
                'parameter' => $parameter,
178 6
                'value'     => $this->checkIfValueIsEloquentModel($value),
179 3
            ]);
180 6
        }
181
182 12
        if (count($parameters) > 0)
183 9
            $routePath->parameters()->saveMany($parameters);
184 12
    }
185
186
    /**
187
     * Check if the value is an eloquent model.
188
     *
189
     * @param  mixed  $value
190
     *
191
     * @return mixed
192
     */
193 6
    private function checkIfValueIsEloquentModel($value)
194
    {
195 6
        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...
196
            foreach ($this->getConfig('routes.model-columns', ['id']) as $column) {
197
                if (
198
                    array_key_exists($column, $attributes = $value->getAttributes()) &&
199
                    ! is_null($attributes[$column])
200
                ) {
201
                    return $attributes[$column];
202
                }
203
            }
204
        }
205
206 6
        return $value;
207
    }
208
}
209