-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add love:reaction-type-add command (#51)
Add love:reaction-type-add console command
- Loading branch information
1 parent
115cf18
commit 6727e21
Showing
7 changed files
with
489 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Love. | ||
* | ||
* (c) Anton Komarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cog\Laravel\Love\Console\Commands; | ||
|
||
use Cog\Laravel\Love\ReactionType\Models\ReactionType; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Str; | ||
|
||
final class ReactionTypeAdd extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'love:reaction-type-add | ||
{--default} | ||
{name?} | ||
{weight?}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Add Reaction Type to Laravel Love'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle(): int | ||
{ | ||
if ($this->option('default')) { | ||
$this->createDefaultReactionTypes(); | ||
|
||
return 0; | ||
} | ||
|
||
$name = $this->resolveName(); | ||
$name = $this->sanitizeName($name); | ||
|
||
if ($this->isNameInvalid($name)) { | ||
$this->error(sprintf( | ||
'Reaction type with name `%s` is invalid.', | ||
$name | ||
)); | ||
|
||
return 1; | ||
} | ||
|
||
if ($this->isReactionTypeNameExists($name)) { | ||
$this->error(sprintf( | ||
'Reaction type with name `%s` already exists.', | ||
$name | ||
)); | ||
|
||
return 1; | ||
} | ||
|
||
$this->createReactionType($name, $this->resolveWeight()); | ||
|
||
return 0; | ||
} | ||
|
||
private function createDefaultReactionTypes(): void | ||
{ | ||
$types = [ | ||
[ | ||
'name' => 'Like', | ||
'weight' => 1, | ||
], | ||
[ | ||
'name' => 'Dislike', | ||
'weight' => -1, | ||
], | ||
]; | ||
|
||
foreach ($types as $type) { | ||
if ($this->isReactionTypeNameExists($type['name'])) { | ||
continue; | ||
} | ||
|
||
$this->createReactionType($type['name'], $type['weight']); | ||
} | ||
} | ||
|
||
private function createReactionType(string $name, int $weight): void | ||
{ | ||
ReactionType::query()->create([ | ||
'name' => $name, | ||
'weight' => $weight, | ||
]); | ||
|
||
$this->line(sprintf( | ||
'Reaction type with name `%s` and weight `%d` was added.', | ||
$name, | ||
$weight | ||
)); | ||
} | ||
|
||
private function resolveName(): string | ||
{ | ||
return $this->argument('name') | ||
?? $this->ask('How to name reaction type?') | ||
?? $this->resolveName(); | ||
} | ||
|
||
private function resolveWeight(): int | ||
{ | ||
return intval($this->argument('weight') ?? $this->ask('What is the weight of this reaction type?')); | ||
} | ||
|
||
private function sanitizeName(string $name): string | ||
{ | ||
$name = trim($name); | ||
$name = Str::studly($name); | ||
|
||
return $name; | ||
} | ||
|
||
private function isReactionTypeNameExists(string $name): bool | ||
{ | ||
return ReactionType::query()->where('name', $name)->exists(); | ||
} | ||
|
||
private function isNameInvalid(string $name): bool | ||
{ | ||
return preg_match('#^[A-Z][a-zA-Z0-9_]*$#', $name) === 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.