generated from vormkracht10/laravel-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathSeoScanUrl.php
94 lines (72 loc) · 3.4 KB
/
SeoScanUrl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace Vormkracht10\Seo\Commands;
use Illuminate\Console\Command;
use Vormkracht10\Seo\Facades\Seo;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\progress;
use function Laravel\Prompts\text;
class SeoScanUrl extends Command
{
public $signature = 'seo:scan-url {url?} {--javascript}';
public $description = 'Scan the SEO score of a url';
public function handle(): int
{
$url = $this->argument('url') ?? text(
label: 'Pleaes enter the url',
validate: function (string $value) {
try {
if (! \Illuminate\Support\Facades\Http::get($value)->successful()) {
return 'Please enter a valid url.';
}
return null;
} catch (\Exception $e) {
return 'Please enter a valid url.';
}
}
);
$useJavascript = $this->option('javascript') ?
$this->option('javascript') :
confirm(
label: 'Do you want to use JavaScript?',
default: true,
yes: 'I do',
no: 'I dont'
);
$progress = progress(label: 'Please wait while we scan your web page...', steps: getCheckCount(), hint: $url);
$progress->start();
$score = Seo::check($url, $progress, $useJavascript);
$progress->finish();
$this->line('');
$this->line('');
$this->line('-----------------------------------------------------------------------------------------------------------------------------------');
$this->line('> '.$url.' | <fg=green>'.$score->getSuccessfulChecks()->count().' passed</> <fg=red>'.($score->getFailedChecks()->count().' failed</>'));
$this->line('-----------------------------------------------------------------------------------------------------------------------------------');
$this->line('');
// If successful and failed checks are empty, we can assume that the
// visit page threw an exception. In that case, we don't want to
// show the checks. But show the exception message instead.
if ($score->getSuccessfulChecks()->isEmpty() && $score->getFailedChecks()->isEmpty()) {
$this->line('<fg=red>✘ Unfortunately, the url you entered is not correct. Please try again with a different url.</>');
return self::FAILURE;
}
$score->getAllChecks()->each(function ($checks, $type) {
$checks->each(function ($check) use ($type) {
if ($type == 'failed') {
$this->line('<fg=red>✘ '.$check->title.' failed.</>');
if (property_exists($check, 'failureReason')) {
$this->line($check->failureReason.' Estimated time to fix: '.$check->timeToFix.' minute(s).');
$this->line('');
}
} else {
$this->line('<fg=green>✔ '.$check->title.'</>');
}
});
$this->line('');
});
$totalChecks = $score->getFailedChecks()->count() + $score->getSuccessfulChecks()->count();
$this->info('Completed '.$totalChecks.' out of '.getCheckCount().' checks.');
$this->line('');
cache()->driver(config('seo.cache.driver'))->tags('seo')->flush();
return self::SUCCESS;
}
}