Skip to content

Commit

Permalink
Add love:reaction-type-add command (#51)
Browse files Browse the repository at this point in the history
Add love:reaction-type-add console command
  • Loading branch information
antonkomarev authored May 7, 2019
1 parent 115cf18 commit 6727e21
Show file tree
Hide file tree
Showing 7 changed files with 489 additions and 37 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to `laravel-love` will be documented in this file.

## [6.1.0] - 2019-05-07

### Added

- ([#51](https://github.com/cybercog/laravel-love/pull/51)) `love:reaction-type-add` console command was added

## [6.0.1] - 2019-03-05

### Removed
Expand Down Expand Up @@ -230,6 +236,7 @@ Follow [upgrade instructions](UPGRADING.md#from-v5-to-v6) to migrate database to

- Initial release

[6.1.0]: https://github.com/cybercog/laravel-love/compare/6.0.1...6.1.0
[6.0.1]: https://github.com/cybercog/laravel-love/compare/6.0.0...6.0.1
[6.0.0]: https://github.com/cybercog/laravel-love/compare/5.2.0...6.0.0
[5.2.0]: https://github.com/cybercog/laravel-love/compare/5.1.1...5.2.0
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ If you want to make changes in migrations, publish them to your application firs
$ php artisan vendor:publish --tag=love-migrations
```

After installing Love, add reaction types using the `love:reaction-type-add` Artisan command.
Or add default `Like` & `Dislike` types using `--default` option.

```sh
$ php artisan love:reaction-type-add --default
```

## Integration

To start using package you need to have:
Expand Down Expand Up @@ -811,6 +818,28 @@ $ love:recount --model="article" --type="Dislike"
$ love:recount --model="App\Models\Article" --type="Dislike"
```

#### Add reaction type

```sh
$ love:reaction-type-add
```

> Note: Type names transformed to StudlyCase. Name `very-good` will be converted to `VeryGood`.
#### Add reaction type without interaction

```sh
$ love:reaction-type-add name=Hate weight=-4
```

#### Add default reaction types

Creates `Like` with weight `1` and `Dislike` with weight `-1`.

```sh
$ love:reaction-type-add --default
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
143 changes: 143 additions & 0 deletions src/Console/Commands/ReactionTypeAdd.php
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;
}
}
7 changes: 2 additions & 5 deletions src/Console/Commands/Recount.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Cog\Laravel\Love\Reactant\ReactionCounter\Services\ReactionCounterService;
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
use Illuminate\Console\Command;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Relations\Relation;

final class Recount extends Command
Expand All @@ -42,14 +41,12 @@ final class Recount extends Command
/**
* Execute the console command.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*
* @throws \Cog\Contracts\Love\Reactable\Exceptions\ReactableInvalid
*/
public function handle(
Dispatcher $events
): void {
public function handle(): void
{
if ($reactableType = $this->argument('reactableType')) {
$reactableType = $this->normalizeReactableModelType($reactableType);
}
Expand Down
2 changes: 2 additions & 0 deletions src/LoveServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Cog\Laravel\Love;

use Cog\Laravel\Love\Console\Commands\ReactionTypeAdd;
use Cog\Laravel\Love\Console\Commands\Recount;
use Cog\Laravel\Love\Console\Commands\UpgradeV5ToV6;
use Cog\Laravel\Love\Reactant\Listeners\DecrementAggregates;
Expand Down Expand Up @@ -68,6 +69,7 @@ private function registerConsoleCommands(): void
{
if ($this->app->runningInConsole()) {
$this->commands([
ReactionTypeAdd::class,
Recount::class,
UpgradeV5ToV6::class,
]);
Expand Down
Loading

0 comments on commit 6727e21

Please sign in to comment.