reliqarts /
laravel-scavenger
| 1 | <?php |
||||
| 2 | |||||
| 3 | /** |
||||
| 4 | * @noinspection PhpMissingFieldTypeInspection |
||||
| 5 | */ |
||||
| 6 | |||||
| 7 | declare(strict_types=1); |
||||
| 8 | |||||
| 9 | namespace ReliqArts\Scavenger\Console\Command; |
||||
| 10 | |||||
| 11 | use Carbon\Carbon; |
||||
| 12 | use Exception; |
||||
| 13 | use Illuminate\Console\Command; |
||||
| 14 | use ReliqArts\Scavenger\Contract\ConfigProvider; |
||||
| 15 | use ReliqArts\Scavenger\Contract\Seeker; |
||||
| 16 | use ReliqArts\Scavenger\Exception\BadDaemonConfig; |
||||
| 17 | use ReliqArts\Scavenger\OptionSet; |
||||
| 18 | use ReliqArts\Scavenger\Result; |
||||
| 19 | |||||
| 20 | class Seek extends Command |
||||
| 21 | { |
||||
| 22 | /** |
||||
| 23 | * The name and signature of the console command. |
||||
| 24 | * |
||||
| 25 | * @var string |
||||
| 26 | */ |
||||
| 27 | protected $signature = 'scavenger:seek |
||||
| 28 | {target? : Optionally specify a single target from list of available targets} |
||||
| 29 | {--w|keywords= : Comma separated keywords} |
||||
| 30 | {--k|keep : Whether to save found scraps} |
||||
| 31 | {--c|convert : Whether to convert found scraps to target objects} |
||||
| 32 | {--y|y : Whether to skip confirmation} |
||||
| 33 | {--b|backoff=0 : Wait time after each scrape} |
||||
| 34 | {--p|pages=2 : Max. number of pages to scrape} |
||||
| 35 | '; |
||||
| 36 | |||||
| 37 | /** |
||||
| 38 | * The console command description. |
||||
| 39 | * |
||||
| 40 | * @var string |
||||
| 41 | */ |
||||
| 42 | protected $description = 'Finds entities and updates database'; |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * Execute the console command. |
||||
| 46 | * |
||||
| 47 | * @throws Exception |
||||
| 48 | */ |
||||
| 49 | public function handle(ConfigProvider $configProvider, Seeker $seeker): void |
||||
| 50 | { |
||||
| 51 | $saveScraps = $this->option('keep'); |
||||
| 52 | $target = $this->argument('target'); |
||||
| 53 | $keywords = $this->option('keywords'); |
||||
| 54 | $skipConfirmation = $this->option('y'); |
||||
| 55 | $backOff = (int)$this->option('backoff'); |
||||
| 56 | $pages = (int)$this->option('pages'); |
||||
| 57 | $convertScraps = $this->option('convert'); |
||||
| 58 | $optionSet = new OptionSet($saveScraps, $convertScraps, $backOff, $pages, $keywords); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
$saveScraps of type string is incompatible with the type boolean expected by parameter $saveScraps of ReliqArts\Scavenger\OptionSet::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 59 | $confirmationQuestion = 'Scavenger will scour a resource for scraps and make model records,' |
||||
| 60 | . "performing HTTP, DB and I/O operations.\n Ensure your internet connection is stable. Ready?"; |
||||
| 61 | |||||
| 62 | $this->comment( |
||||
| 63 | PHP_EOL |
||||
| 64 | . "<info>♣♣♣</info> Scavenger Seek \nHelp is here, try: php artisan scavenger:seek --help" |
||||
| 65 | ); |
||||
| 66 | |||||
| 67 | if (!($skipConfirmation || $this->confirm($confirmationQuestion))) { |
||||
| 68 | return; |
||||
| 69 | } |
||||
| 70 | |||||
| 71 | if (!$this->logDaemonIn($configProvider)) { |
||||
| 72 | return; |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | $this->info( |
||||
| 76 | "Scavenger is seeking. Output is shown below.\nT: " |
||||
| 77 | . Carbon::now()->toCookieString() . "\n----------" |
||||
| 78 | ); |
||||
| 79 | |||||
| 80 | $result = $seeker->seek($optionSet, $target); |
||||
|
0 ignored issues
–
show
It seems like
$target can also be of type string[]; however, parameter $targetName of ReliqArts\Scavenger\Contract\Seeker::seek() does only seem to accept null|string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 81 | |||||
| 82 | $this->showResult($result); |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | private function logDaemonIn(ConfigProvider $configProvider): bool |
||||
| 86 | { |
||||
| 87 | try { |
||||
| 88 | $daemon = $configProvider->getDaemon(); |
||||
| 89 | |||||
| 90 | auth()->login($daemon); |
||||
| 91 | |||||
| 92 | return true; |
||||
| 93 | } catch (BadDaemonConfig $exception) { |
||||
| 94 | $this->line( |
||||
| 95 | PHP_EOL |
||||
| 96 | . "<error>✘ Woe there! Scavenger daemon doesn't live in your database and couldn't be created. " |
||||
| 97 | . "You sure you know what yer doin'?</error>\n► " |
||||
| 98 | . $exception->getMessage() |
||||
| 99 | ); |
||||
| 100 | |||||
| 101 | return false; |
||||
| 102 | } |
||||
| 103 | } |
||||
| 104 | |||||
| 105 | private function showResult(Result $result): void |
||||
| 106 | { |
||||
| 107 | if (!$result->isSuccess()) { |
||||
| 108 | foreach ($result->getErrors() as $errorMessage) { |
||||
| 109 | $this->line(PHP_EOL . "<error>✘</error> {$errorMessage}"); |
||||
| 110 | } |
||||
| 111 | |||||
| 112 | return; |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | try { |
||||
| 116 | $this->info(PHP_EOL . '----------'); |
||||
| 117 | $this->comment('<info>✔</info> Done. Scavenger daemon now goes to sleep...'); |
||||
| 118 | $this->line(''); |
||||
| 119 | |||||
| 120 | $extra = $result->getExtra(); |
||||
| 121 | $headers = ['Time', 'Scraps Found', 'New', 'Saved?', 'Converted?']; |
||||
| 122 | $data = [ |
||||
| 123 | [ |
||||
| 124 | $extra->executionTime, |
||||
| 125 | $extra->total, |
||||
| 126 | $extra->new, |
||||
| 127 | $extra->scrapsSaved ? 'true' : 'false', |
||||
| 128 | $extra->scrapsConverted ? 'true' : 'false', |
||||
| 129 | ], |
||||
| 130 | ]; |
||||
| 131 | |||||
| 132 | $this->table($headers, $data); |
||||
| 133 | $this->line(PHP_EOL); |
||||
| 134 | } catch (Exception $exception) { |
||||
| 135 | $this->line( |
||||
| 136 | PHP_EOL |
||||
| 137 | . "<error>✘</error> Something strange happened at the end there... {$exception->getMessage()}" |
||||
| 138 | ); |
||||
| 139 | } |
||||
| 140 | } |
||||
| 141 | } |
||||
| 142 |