Completed
Pull Request — master (#2)
by ARCANEDEV
09:30
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
    }
48
49
    /**
50
     * Track the matched route.
51
     *
52
     * @param  \Illuminate\Routing\Route  $route
53
     * @param  \Illuminate\Http\Request   $request
54
     *
55
     * @return int
56
     */
57 12
    public function track(Route $route, Request $request)
58
    {
59 12
        return $this->trackRoutePath(
60 12
            $route, $request, $this->trackRoute($route)
61 6
        );
62
    }
63
64
    /* ------------------------------------------------------------------------------------------------
65
     |  Other Functions
66
     | ------------------------------------------------------------------------------------------------
67
     */
68
    /**
69
     * Track the current route.
70
     *
71
     * @param  \Illuminate\Routing\Route  $route
72
     *
73
     * @return int
74
     */
75 12
    private function trackRoute(Route $route)
76
    {
77 12
        return $this->getModel()
78 12
                    ->firstOrCreate([
79 12
                        'name'   => $this->getRouteName($route),
80 12
                        'action' => $route->getActionName(),
81 12
                    ])->id;
82
    }
83
84
    /**
85
     * Check if the route is ignored by a route pattern.
86
     *
87
     * @param  \Illuminate\Routing\Route  $route
88
     *
89
     * @return bool
90
     */
91 18
    private function isInIgnoredRouteNames($route)
92
    {
93
        if (
94 18
            ! is_null($name  = $route->getName()) &&
95 18
            count($names = $this->getConfig('routes.ignore.names', [])) > 0
96 9
        ) {
97 18
            foreach ($names as $pattern) {
98 18
                if (Str::is($pattern, $name)) return true;
99 9
            }
100 9
        }
101
102 18
        return false;
103
    }
104
105
    /**
106
     * Get the route name.
107
     *
108
     * @param  \Illuminate\Routing\Route  $route
109
     *
110
     * @return string
111
     */
112 12
    private function getRouteName(Route $route)
113
    {
114 12
        if ($name = $route->getName())
115 12
            return $name;
116
117
        if ($name = Arr::get($route->getAction(), 'as'))
118
            return $name;
119
120
        return $route->getUri();
121
    }
122
123
    /**
124
     * Track the route path.
125
     *
126
     * @param  \Illuminate\Routing\Route  $route
127
     * @param  \Illuminate\Http\Request   $request
128
     * @param  int                        $routeId
129
     *
130
     * @return int
131
     */
132 12
    private function trackRoutePath(Route $route, Request $request, $routeId)
133
    {
134
        /** @var  \Arcanedev\LaravelTracker\Models\RoutePath  $model */
135 12
        $model = $this->makeModel(AbstractModel::MODEL_ROUTE_PATH)->firstOrCreate([
136 12
            'route_id' => $routeId,
137 12
            'path'     => $request->path(),
138 6
        ]);
139
140 12
        if ($model->wasRecentlyCreated)
141 12
            $this->trackRoutePathParameters($route, $model);
142
143 12
        return $model->id;
144
    }
145
146
    /**
147
     * Track the route path parameters.
148
     *
149
     * @param  \Illuminate\Routing\Route                   $route
150
     * @param  \Arcanedev\LaravelTracker\Models\RoutePath  $routePath
151
     */
152 12
    private function trackRoutePathParameters(Route $route, Models\RoutePath $routePath)
153
    {
154 12
        $parameters = [];
155
156 12
        if (count($params = $route->parameters()) > 0) {
157 6
            foreach ($params as $parameter => $value) {
158 6
                $parameters[] = $this->makeModel(AbstractModel::MODEL_ROUTE_PATH_PARAMETER)->fill([
159 6
                    'parameter' => $parameter,
160 6
                    'value'     => $this->checkIfValueIsEloquentModel($value),
161 3
                ]);
162 3
            }
163
164 6
            $routePath->parameters()->saveMany($parameters);
165 3
        }
166 12
    }
167
168
    /**
169
     * Check if the value is an eloquent model.
170
     *
171
     * @param  mixed  $value
172
     *
173
     * @return mixed
174
     */
175 6
    private function checkIfValueIsEloquentModel($value)
176
    {
177 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...
178
            foreach ($this->getConfig('routes.model-columns', ['id']) as $column) {
179
                if (
180
                    array_key_exists($column, $attributes = $value->getAttributes()) &&
181
                    ! is_null($attributes[$column])
182
                ) {
183
                    return $attributes[$column];
184
                }
185
            }
186
        }
187
188 6
        return $value;
189
    }
190
}
191