Skip to content

Commit 6727e21

Browse files
authored
Add love:reaction-type-add command (#51)
Add love:reaction-type-add console command
1 parent 115cf18 commit 6727e21

File tree

7 files changed

+489
-37
lines changed

7 files changed

+489
-37
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

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

5+
## [6.1.0] - 2019-05-07
6+
7+
### Added
8+
9+
- ([#51](https://github.com/cybercog/laravel-love/pull/51)) `love:reaction-type-add` console command was added
10+
511
## [6.0.1] - 2019-03-05
612

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

231237
- Initial release
232238

239+
[6.1.0]: https://github.com/cybercog/laravel-love/compare/6.0.1...6.1.0
233240
[6.0.1]: https://github.com/cybercog/laravel-love/compare/6.0.0...6.0.1
234241
[6.0.0]: https://github.com/cybercog/laravel-love/compare/5.2.0...6.0.0
235242
[5.2.0]: https://github.com/cybercog/laravel-love/compare/5.1.1...5.2.0

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ If you want to make changes in migrations, publish them to your application firs
129129
$ php artisan vendor:publish --tag=love-migrations
130130
```
131131

132+
After installing Love, add reaction types using the `love:reaction-type-add` Artisan command.
133+
Or add default `Like` & `Dislike` types using `--default` option.
134+
135+
```sh
136+
$ php artisan love:reaction-type-add --default
137+
```
138+
132139
## Integration
133140

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

821+
#### Add reaction type
822+
823+
```sh
824+
$ love:reaction-type-add
825+
```
826+
827+
> Note: Type names transformed to StudlyCase. Name `very-good` will be converted to `VeryGood`.
828+
829+
#### Add reaction type without interaction
830+
831+
```sh
832+
$ love:reaction-type-add name=Hate weight=-4
833+
```
834+
835+
#### Add default reaction types
836+
837+
Creates `Like` with weight `1` and `Dislike` with weight `-1`.
838+
839+
```sh
840+
$ love:reaction-type-add --default
841+
```
842+
814843
## Changelog
815844

816845
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Love.
5+
*
6+
* (c) Anton Komarev <a.komarev@cybercog.su>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Cog\Laravel\Love\Console\Commands;
15+
16+
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
17+
use Illuminate\Console\Command;
18+
use Illuminate\Support\Str;
19+
20+
final class ReactionTypeAdd extends Command
21+
{
22+
/**
23+
* The name and signature of the console command.
24+
*
25+
* @var string
26+
*/
27+
protected $signature = 'love:reaction-type-add
28+
{--default}
29+
{name?}
30+
{weight?}';
31+
32+
/**
33+
* The console command description.
34+
*
35+
* @var string
36+
*/
37+
protected $description = 'Add Reaction Type to Laravel Love';
38+
39+
/**
40+
* Execute the console command.
41+
*
42+
* @return int
43+
*/
44+
public function handle(): int
45+
{
46+
if ($this->option('default')) {
47+
$this->createDefaultReactionTypes();
48+
49+
return 0;
50+
}
51+
52+
$name = $this->resolveName();
53+
$name = $this->sanitizeName($name);
54+
55+
if ($this->isNameInvalid($name)) {
56+
$this->error(sprintf(
57+
'Reaction type with name `%s` is invalid.',
58+
$name
59+
));
60+
61+
return 1;
62+
}
63+
64+
if ($this->isReactionTypeNameExists($name)) {
65+
$this->error(sprintf(
66+
'Reaction type with name `%s` already exists.',
67+
$name
68+
));
69+
70+
return 1;
71+
}
72+
73+
$this->createReactionType($name, $this->resolveWeight());
74+
75+
return 0;
76+
}
77+
78+
private function createDefaultReactionTypes(): void
79+
{
80+
$types = [
81+
[
82+
'name' => 'Like',
83+
'weight' => 1,
84+
],
85+
[
86+
'name' => 'Dislike',
87+
'weight' => -1,
88+
],
89+
];
90+
91+
foreach ($types as $type) {
92+
if ($this->isReactionTypeNameExists($type['name'])) {
93+
continue;
94+
}
95+
96+
$this->createReactionType($type['name'], $type['weight']);
97+
}
98+
}
99+
100+
private function createReactionType(string $name, int $weight): void
101+
{
102+
ReactionType::query()->create([
103+
'name' => $name,
104+
'weight' => $weight,
105+
]);
106+
107+
$this->line(sprintf(
108+
'Reaction type with name `%s` and weight `%d` was added.',
109+
$name,
110+
$weight
111+
));
112+
}
113+
114+
private function resolveName(): string
115+
{
116+
return $this->argument('name')
117+
?? $this->ask('How to name reaction type?')
118+
?? $this->resolveName();
119+
}
120+
121+
private function resolveWeight(): int
122+
{
123+
return intval($this->argument('weight') ?? $this->ask('What is the weight of this reaction type?'));
124+
}
125+
126+
private function sanitizeName(string $name): string
127+
{
128+
$name = trim($name);
129+
$name = Str::studly($name);
130+
131+
return $name;
132+
}
133+
134+
private function isReactionTypeNameExists(string $name): bool
135+
{
136+
return ReactionType::query()->where('name', $name)->exists();
137+
}
138+
139+
private function isNameInvalid(string $name): bool
140+
{
141+
return preg_match('#^[A-Z][a-zA-Z0-9_]*$#', $name) === 0;
142+
}
143+
}

src/Console/Commands/Recount.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Cog\Laravel\Love\Reactant\ReactionCounter\Services\ReactionCounterService;
2121
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
2222
use Illuminate\Console\Command;
23-
use Illuminate\Contracts\Events\Dispatcher;
2423
use Illuminate\Database\Eloquent\Relations\Relation;
2524

2625
final class Recount extends Command
@@ -42,14 +41,12 @@ final class Recount extends Command
4241
/**
4342
* Execute the console command.
4443
*
45-
* @param \Illuminate\Contracts\Events\Dispatcher $events
4644
* @return void
4745
*
4846
* @throws \Cog\Contracts\Love\Reactable\Exceptions\ReactableInvalid
4947
*/
50-
public function handle(
51-
Dispatcher $events
52-
): void {
48+
public function handle(): void
49+
{
5350
if ($reactableType = $this->argument('reactableType')) {
5451
$reactableType = $this->normalizeReactableModelType($reactableType);
5552
}

src/LoveServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Cog\Laravel\Love;
1515

16+
use Cog\Laravel\Love\Console\Commands\ReactionTypeAdd;
1617
use Cog\Laravel\Love\Console\Commands\Recount;
1718
use Cog\Laravel\Love\Console\Commands\UpgradeV5ToV6;
1819
use Cog\Laravel\Love\Reactant\Listeners\DecrementAggregates;
@@ -68,6 +69,7 @@ private function registerConsoleCommands(): void
6869
{
6970
if ($this->app->runningInConsole()) {
7071
$this->commands([
72+
ReactionTypeAdd::class,
7173
Recount::class,
7274
UpgradeV5ToV6::class,
7375
]);

0 commit comments

Comments
 (0)