This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Leitom\Geo; |
||
4 | |||
5 | use Illuminate\Support\Arr; |
||
6 | use Leitom\Geo\Facades\Geo; |
||
7 | use Illuminate\Support\Facades\DB; |
||
8 | use Leitom\Geo\Events\ModelsRemoved; |
||
9 | use Leitom\Geo\Events\ModelsImported; |
||
10 | use Illuminate\Support\Collection as BaseCollection; |
||
11 | |||
12 | trait HasGeoAbilities |
||
13 | { |
||
14 | public $geoEnabled = true; |
||
15 | |||
16 | public static function bootHasGeoAbilities() |
||
17 | { |
||
18 | static::addGlobalScope(new GeoScope); |
||
19 | |||
20 | static::observe(new ModelObserver); |
||
21 | |||
22 | (new static)->registerGeoMacros(); |
||
23 | } |
||
24 | |||
25 | public function registerGeoMacros() |
||
26 | { |
||
27 | $self = $this; |
||
28 | |||
29 | BaseCollection::macro('addGeo', function () use ($self) { |
||
30 | $self->addGeo($this); |
||
31 | }); |
||
32 | |||
33 | BaseCollection::macro('removeGeo', function () use ($self) { |
||
34 | $self->removeGeo($this); |
||
35 | }); |
||
36 | } |
||
37 | |||
38 | public function addGeo($models) |
||
39 | { |
||
40 | if ($models->isEmpty()) { |
||
41 | return; |
||
42 | } |
||
43 | |||
44 | Geo::index($models->first()->geoIndex())->add($models->map->toCoordinate()); |
||
45 | |||
46 | event(new ModelsImported($models)); |
||
47 | } |
||
48 | |||
49 | public function removeGeo($models) |
||
50 | { |
||
51 | if ($models->isEmpty()) { |
||
52 | return; |
||
53 | } |
||
54 | |||
55 | Geo::index($models->first()->geoIndex())->remove($models->map->geoKey()->all()); |
||
56 | |||
57 | event(new ModelsRemoved($models)); |
||
58 | } |
||
59 | |||
60 | public static function geoImportAll() |
||
61 | { |
||
62 | $self = new static; |
||
63 | |||
64 | $self->newQuery()->orderBy($self->geoKeyName())->addGeo(); |
||
65 | } |
||
66 | |||
67 | public static function geoRemoveAll() |
||
68 | { |
||
69 | $self = new static; |
||
70 | |||
71 | $self->newQuery()->orderBy($self->geoKeyName())->removeGeo(); |
||
72 | } |
||
73 | |||
74 | public function geoDistanceFrom($model) |
||
75 | { |
||
76 | return [ |
||
77 | 'unit' => $this->geoUnit, |
||
78 | 'distance' => (float) Geo::between($this->toCoordinate(), $model->toCoordinate()), |
||
79 | ]; |
||
80 | } |
||
81 | |||
82 | public function scopeGeoNearest($builder, $radius = 10) |
||
83 | { |
||
84 | $locations = array_keys(Geo::index($this->geoIndex())->from($this->toCoordinate(), $radius)); |
||
85 | |||
86 | $builder->whereIn($this->geoKeyName(), $locations); |
||
87 | |||
88 | View Code Duplication | if ($this->databaseDriver() !== 'sqlite' && ! empty($locations)) { |
|
0 ignored issues
–
show
|
|||
89 | $builder->orderByRaw(DB::raw(sprintf('FIELD(%s,%s)', $this->geoKeyName(), implode(',', $locations)))); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | public function scopeGeoSearch($builder, $longitude, $latitude, $radius, $sort = 'ASC') |
||
94 | { |
||
95 | $locations = array_keys(Geo::index($this->geoIndex())->search($longitude, $latitude, $radius, $sort)); |
||
96 | |||
97 | $builder->whereIn($this->geoKeyName(), $locations); |
||
98 | |||
99 | View Code Duplication | if ($this->databaseDriver() !== 'sqlite' && ! empty($locations)) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
100 | $builder->orderByRaw(DB::raw(sprintf('FIELD(%s,%s)', $this->geoKeyName(), implode(',', $locations)))); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | public function getGeoUnitAttribute() |
||
105 | { |
||
106 | return config('geo.unit'); |
||
107 | } |
||
108 | |||
109 | public function getGeoDistanceAttribute() |
||
110 | { |
||
111 | if ($this->geoEnabled() && array_key_exists($this->geoKey(), $results = Geo::previousGeoSearch($this->geoIndex()))) { |
||
112 | return Arr::get($results, $this->geoKey()); |
||
113 | } |
||
114 | |||
115 | return 0; |
||
116 | } |
||
117 | |||
118 | public function geoEnabled() |
||
119 | { |
||
120 | return $this->geoEnabled; |
||
121 | } |
||
122 | |||
123 | public function geoIndex() |
||
124 | { |
||
125 | return $this->getTable(); |
||
126 | } |
||
127 | |||
128 | public function geoKeyName() |
||
129 | { |
||
130 | return $this->getKeyName(); |
||
131 | } |
||
132 | |||
133 | public function geoKey() |
||
134 | { |
||
135 | return $this->getKey(); |
||
136 | } |
||
137 | |||
138 | public function geoLongitude() |
||
139 | { |
||
140 | return $this->longitude; |
||
141 | } |
||
142 | |||
143 | public function geoLatitude() |
||
144 | { |
||
145 | return $this->latitude; |
||
146 | } |
||
147 | |||
148 | public function toCoordinate() |
||
149 | { |
||
150 | return new Coordinate($this->geoKey(), $this->geoLongitude(), $this->geoLatitude()); |
||
151 | } |
||
152 | |||
153 | public function toArray() |
||
154 | { |
||
155 | $attributes = parent::toArray(); |
||
156 | |||
157 | if ($this->geoEnabled()) { |
||
158 | $attributes['geo_unit'] = $this->geoUnit; |
||
159 | $attributes['geo_distance'] = $this->geoDistance; |
||
160 | } |
||
161 | |||
162 | return $attributes; |
||
163 | } |
||
164 | |||
165 | protected function databaseDriver() |
||
166 | { |
||
167 | $connection = config('database.default'); |
||
168 | |||
169 | return config("database.connections.{$connection}.driver"); |
||
170 | } |
||
171 | } |
||
172 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.