Skip to content

Commit 6c736fa

Browse files
committed
Refactor to remove some complexity
Fixes #4
1 parent a21b7e6 commit 6c736fa

File tree

4 files changed

+73
-54
lines changed

4 files changed

+73
-54
lines changed

phpinsights.config.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,15 @@
8585
PropertyTypeHintSniff::class,
8686
ReturnTypeHintSniff::class,
8787
UselessFunctionDocCommentSniff::class,
88+
// \NunoMaduro\PhpInsights\Domain\Metrics\Complexity\Complexity::class,
8889
],
8990

9091
'config' => [
9192
ForbiddenPrivateMethods::class => [
9293
'title' => 'The usage of private methods is not idiomatic in Laravel.',
9394
],
9495
\NunoMaduro\PhpInsights\Domain\Insights\CyclomaticComplexityIsHigh::class => [
95-
'maxComplexity' => 20,
96+
'maxComplexity' => 10,
9697
],
9798

9899
],
@@ -110,7 +111,7 @@
110111

111112
'requirements' => [
112113
'min-quality' => 80,
113-
'min-complexity' => 80,
114+
'min-complexity' => 65,
114115
'min-architecture' => 80,
115116
'min-style' => 80,
116117
'disable-security-check' => true,

src/HasUniqueUrlAttributes.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Vlados\LaravelUniqueUrls;
4+
5+
trait HasUniqueUrlAttributes
6+
{
7+
public function initializeHasUniqueUrlTrait(): void
8+
{
9+
$this->append('relative_url');
10+
$this->append('absolute_url');
11+
$this->makeVisible('relative_url');
12+
$this->makeVisible('absolute_url');
13+
}
14+
15+
public function getRelativeUrlAttribute(): string
16+
{
17+
return $this->getUrl(false);
18+
}
19+
20+
public function getAbsoluteUrlAttribute(): string
21+
{
22+
return $this->getUrl(true);
23+
}
24+
25+
/**
26+
* Returns the absolute url for the model.
27+
*
28+
* @return string
29+
*
30+
* @throws Throwable
31+
*/
32+
private function getUrl($absolute = true): string
33+
{
34+
$url = $this->url()->where('language', app()->getLocale())->first()->slug ?? '';
35+
36+
return $absolute ? url($url) : $url;
37+
}
38+
}

src/HasUniqueUrlTrait.php

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Vlados\LaravelUniqueUrls;
44

55
use Exception;
6-
use Illuminate\Database\Eloquent\Casts\Attribute;
76
use Illuminate\Database\Eloquent\Model;
87
use Illuminate\Database\Eloquent\Relations\MorphOne;
98
use Illuminate\Support\Str;
@@ -64,57 +63,43 @@ public function url(): MorphOne
6463
return $this->morphOne(Url::class, 'related');
6564
}
6665

67-
/**
68-
* Returns the absolute url for the model.
69-
*
70-
* @return string
71-
*
72-
* @throws Throwable
73-
*/
74-
private function getUrl($absolute = true): string
75-
{
76-
$url = $this->url()->where('language', app()->getLocale())->first()->slug ?? '';
77-
78-
return $absolute ? url($url) : $url;
79-
}
80-
8166
public function urlStrategy(): string
8267
{
8368
return Str::slug($this->getAttribute('name'));
8469
}
8570

86-
/**
87-
* Generate automatically the urls on create. You can disable it and manually trigger it after.
88-
*
89-
* @return bool
90-
*/
9171
public function isAutoGenerateUrls(): bool
9272
{
9373
return $this->autoGenerateUrls;
9474
}
9575

96-
/**
97-
* @param bool $autoGenerateUrls
98-
*/
9976
public function setAutoGenerateUrls(bool $autoGenerateUrls): void
10077
{
10178
$this->autoGenerateUrls = $autoGenerateUrls;
10279
}
10380

81+
public function getRelativeUrlAttribute(): string
82+
{
83+
return $this->getUrl(false);
84+
}
85+
86+
public function getAbsoluteUrlAttribute(): string
87+
{
88+
return $this->getUrl(true);
89+
}
90+
10491
protected static function bootHasUniqueUrlTrait(): void
10592
{
10693
static::created(function (Model $model) {
107-
if ($model->isAutoGenerateUrls() === false) {
108-
return;
94+
if ($model->isAutoGenerateUrls() === true) {
95+
$model->generateUrl();
10996
}
110-
$model->generateUrl();
11197
});
11298

11399
static::updated(function (Model $model) {
114-
if ($model->isAutoGenerateUrls() === false) {
115-
return;
100+
if ($model->isAutoGenerateUrls() === true) {
101+
$model->generateUrlOnUpdate();
116102
}
117-
$model->generateUrlOnUpdate();
118103
});
119104
}
120105

@@ -123,29 +108,26 @@ protected function generateUrlOnUpdate(): void
123108
$unique_url = Url::makeSlug($this->urlStrategy(), $this);
124109
$this->url()->get()->each(function (Url $url) use ($unique_url) {
125110
$prefix = config('app.fallback_locale') === $url->getAttribute('language') ? '' : $url->getAttribute('language') . '/';
126-
if ($url->getAttribute('slug') === $prefix . $unique_url) {
127-
return;
111+
if ($url->getAttribute('slug') !== $prefix . $unique_url) {
112+
$url->update([
113+
'slug' => $prefix . $unique_url,
114+
]);
115+
$url->save();
128116
}
129-
130-
$url->update([
131-
'slug' => $prefix . $unique_url,
132-
]);
133-
$url->save();
134117
});
135118
}
136119

137120
/**
138-
* Interact with the user's address.
121+
* Returns the absolute url for the model.
122+
*
123+
* @return string
139124
*
140-
* @return Attribute
125+
* @throws Throwable
141126
*/
142-
public function getRelativeUrlAttribute(): string
127+
private function getUrl($absolute = true): string
143128
{
144-
return $this->getUrl(false);
145-
}
129+
$url = $this->url()->where('language', app()->getLocale())->first()->slug ?? '';
146130

147-
public function getAbsoluteUrlAttribute(): string
148-
{
149-
return $this->getUrl(true);
131+
return $absolute ? url($url) : $url;
150132
}
151133
}

src/LaravelUniqueUrls.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ public function handleRequest(Url $urlObj, Request $request)
2222
return \App::call([$slugController, '__invoke']);
2323
}
2424
$arguments['related'] = $urlObj->related;
25-
if (isset($urlObj->method, $arguments) && method_exists($urlObj->controller, $urlObj->method)) {
26-
$called = $slugController->{$urlObj->method}($request, $arguments);
27-
} elseif (isset($urlObj->method) && ! isset($arguments) && method_exists($urlObj->controller, $urlObj->method)) {
28-
$called = $slugController->{$urlObj->method}($request);
29-
} elseif (! isset($urlObj->method) && isset($arguments) && method_exists($urlObj->controller, 'show')) {
30-
$called = $slugController->show($arguments);
31-
} elseif (! isset($urlObj->method) && ! isset($arguments) && method_exists($urlObj->controller, 'index')) {
32-
$called = $slugController->index($request);
25+
if (method_exists($urlObj->controller, $urlObj->method)) {
26+
$called = $slugController->{$urlObj->method}($request, $arguments ?? []);
27+
} elseif (method_exists($urlObj->controller, 'show')) {
28+
$called = $slugController->show($request, $arguments ?? []);
29+
} elseif (method_exists($urlObj->controller, 'index')) {
30+
$called = $slugController->index($request, $arguments ?? []);
3331
}
3432
if (isset($called) && $called !== false) {
3533
return $called;

0 commit comments

Comments
 (0)