Skip to content

Commit cb2a257

Browse files
committed
feat: LAR-188 integer new module in laravelcm and remove the old one
1 parent 009b64b commit cb2a257

22 files changed

+107
-176
lines changed

app-modules/gamify/composer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@
1010
"autoload": {
1111
"psr-4": {
1212
"Laravelcm\\Gamify\\": "src/",
13-
"Laravelcm\\Gamify\\Tests\\": "tests/",
1413
"Laravelcm\\Gamify\\Database\\Factories\\": "database/factories/",
1514
"Laravelcm\\Gamify\\Database\\Seeders\\": "database/seeders/"
16-
}
15+
},
16+
"files": [
17+
"src/helpers.php"
18+
]
1719
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"Laravelcm\\Gamify\\Tests\\": "tests/"
23+
}
24+
},
1825
"minimum-stability": "stable",
1926
"extra": {
2027
"laravel": {

app-modules/gamify/config/gamify.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
declare(strict_types=1);
44

5+
use Laravelcm\Gamify\Models\Reputation;
6+
57
return [
68

79
'payee_model' => '\App\Models\User',
810

9-
'reputation_model' => '\Laravel\Gamify\Reputation',
11+
'reputation_model' => Reputation::class,
1012

1113
'allow_reputation_duplicate' => true,
1214

app-modules/gamify/src/Console/MakePointCommand.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ final class MakePointCommand extends GeneratorCommand
3131

3232
/**
3333
* Get the stub file for the generator.
34-
*
35-
* @return string
3634
*/
3735
protected function getStub(): string
3836
{
@@ -43,7 +41,6 @@ protected function getStub(): string
4341
* Get the default namespace for the class.
4442
*
4543
* @param string $rootNamespace The root namespace
46-
* @return string
4744
*/
4845
protected function getDefaultNamespace($rootNamespace): string
4946
{

app-modules/gamify/src/Console/stubs/point.stub

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace DummyNamespace;
46

7+
use App\Models\User;
8+
use Illuminate\Database\Eloquent\Model;
59
use Laravelcm\Gamify\PointType;
10+
use Laravelcm\Gamify\Exceptions\PointSubjectNotSetException;
611

7-
class DummyClass extends PointType
12+
final class DummyClass extends PointType
813
{
914
/**
1015
* Number of points
1116
*
1217
* @var int
1318
*/
14-
public $points = 20;
19+
public int $points = 20;
1520

1621
/**
1722
* Point constructor
1823
*
19-
* @param $subject
24+
* @param mixed $subject
2025
*/
21-
public function __construct($subject)
26+
public function __construct(mixed $subject)
2227
{
2328
$this->subject = $subject;
2429
}
@@ -27,8 +32,9 @@ class DummyClass extends PointType
2732
* User who will be receive points
2833
*
2934
* @return mixed
35+
* @throws PointSubjectNotSetException
3036
*/
31-
public function payee()
37+
public function payee(): Model
3238
{
3339
return $this->getSubject()->user;
3440
}

app-modules/gamify/src/Models/Reputation.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ public function payee(): BelongsTo
3131

3232
/**
3333
* Get the owning subject model
34-
*
35-
* @return MorphTo
3634
*/
3735
public function subject(): MorphTo
3836
{

app-modules/gamify/src/PointType.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Laravelcm\Gamify;
66

7-
use App\Models\User;
87
use Illuminate\Database\Eloquent\Model;
98
use Illuminate\Database\Eloquent\Relations\HasMany;
109
use Laravelcm\Gamify\Exceptions\InvalidPayeeModelException;
@@ -16,15 +15,11 @@ abstract class PointType
1615
{
1716
/**
1817
* Subject for reputation
19-
*
20-
* @var Model
2118
*/
2219
protected Model $subject;
2320

2421
/**
2522
* Check qualification to give this point
26-
*
27-
* @return bool
2823
*/
2924
public function qualifier(): bool
3025
{
@@ -34,7 +29,7 @@ public function qualifier(): bool
3429
/**
3530
* Payee who will be receiving points
3631
*
37-
* @return Model
32+
*
3833
* @throws PointSubjectNotSetException
3934
* @throws InvalidPayeeModelException
4035
*/
@@ -50,7 +45,6 @@ public function payee(): Model
5045
/**
5146
* Subject model for point
5247
*
53-
* @return Model
5448
* @throws PointSubjectNotSetException
5549
*/
5650
public function getSubject(): Model
@@ -64,8 +58,6 @@ public function getSubject(): Model
6458

6559
/**
6660
* Get point name
67-
*
68-
* @return string
6961
*/
7062
public function getName(): string
7163
{
@@ -77,7 +69,6 @@ public function getName(): string
7769
/**
7870
* Get points
7971
*
80-
* @return int
8172
*
8273
* @throws PointsNotDefinedException
8374
*/
@@ -92,8 +83,6 @@ public function getPoints(): int
9283

9384
/**
9485
* Set a subject
95-
*
96-
* @param mixed $subject
9786
*/
9887
public function setSubject(mixed $subject): void
9988
{
@@ -103,7 +92,6 @@ public function setSubject(mixed $subject): void
10392
/**
10493
* Check if reputation already exists for a point
10594
*
106-
* @return bool
10795
*
10896
* @throws InvalidPayeeModelException
10997
* @throws PointSubjectNotSetException
@@ -116,7 +104,6 @@ public function reputationExists(): bool
116104
/**
117105
* Get first reputation for point
118106
*
119-
* @return Model
120107
*
121108
* @throws InvalidPayeeModelException
122109
* @throws PointSubjectNotSetException
@@ -129,7 +116,6 @@ public function firstReputation(): Model
129116
/**
130117
* Store a reputation in the database
131118
*
132-
* @param array $meta
133119
* @return mixed
134120
*
135121
* @throws InvalidPayeeModelException
@@ -151,7 +137,6 @@ public function storeReputation(array $meta): Reputation
151137
/**
152138
* Get reputation query for this point
153139
*
154-
* @return HasMany
155140
*
156141
* @throws InvalidPayeeModelException
157142
* @throws PointSubjectNotSetException
@@ -169,7 +154,6 @@ public function reputationQuery(): HasMany
169154
/**
170155
* Return reputations payee relation
171156
*
172-
* @return HasMany<User>
173157
*
174158
* @throws InvalidPayeeModelException
175159
* @throws PointSubjectNotSetException

app-modules/gamify/src/Providers/GamifyServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function boot(): void
1818
{
1919
$this->publishes([
2020
__DIR__.'/../../config/gamify.php' => config_path('gamify.php'),
21-
], 'gamify-config');
21+
], 'gamify');
2222

2323
$this->loadMigrationsFrom(__DIR__.'/../../database/migrations');
2424

app-modules/gamify/src/Traits/HasReputations.php

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use App\Models\User;
88
use Illuminate\Database\Eloquent\Collection;
99
use Illuminate\Database\Eloquent\Relations\HasMany;
10-
use Illuminate\Support\Str;
10+
use Illuminate\Support\Number;
1111
use Laravelcm\Gamify\Exceptions\InvalidPayeeModelException;
1212
use Laravelcm\Gamify\Exceptions\PointsNotDefinedException;
1313
use Laravelcm\Gamify\Exceptions\PointSubjectNotSetException;
@@ -30,14 +30,18 @@ public function givePoint(PointType $pointType): bool
3030
}
3131

3232
if ($this->storeReputation($pointType)) {
33-
return $pointType->payee()->addPoint($pointType->getPoints());
33+
$pointType->payee()->addPoint($pointType->getPoints());
34+
return true;
3435
}
36+
37+
return false;
3538
}
3639

3740
/**
3841
* Undo last given point for a subject model
3942
*
4043
* @throws InvalidPayeeModelException
44+
* @throws PointSubjectNotSetException
4145
*/
4246
public function undoPoint(PointType $pointType): bool
4347
{
@@ -47,12 +51,13 @@ public function undoPoint(PointType $pointType): bool
4751
return false;
4852
}
4953

50-
// undo reputation
5154
$reputation->undo();
5255
}
5356

5457
/**
5558
* Reputations of user relation
59+
*
60+
* @return HasMany<User>
5661
*/
5762
public function reputations(): HasMany
5863
{
@@ -74,16 +79,13 @@ public function storeReputation(PointType $pointType, array $meta = []): bool
7479
return false;
7580
}
7681

77-
return $pointType->storeReputation($meta);
82+
return $pointType->storeReputation($meta) instanceof \Laravelcm\Gamify\Models\Reputation;
7883
}
7984

8085
/**
8186
* Give point to a user
82-
*
83-
* @param int $point
84-
* @return HasReputations
8587
*/
86-
public function addPoint($point = 1)
88+
public function addPoint(int $point = 1): static
8789
{
8890
$this->increment($this->getReputationField(), $point);
8991

@@ -92,11 +94,8 @@ public function addPoint($point = 1)
9294

9395
/**
9496
* Reduce a user point
95-
*
96-
* @param int $point
97-
* @return HasReputations
9897
*/
99-
public function reducePoint($point = 1)
98+
public function reducePoint(int $point = 1): static
10099
{
101100
$this->decrement($this->getReputationField(), $point);
102101

@@ -105,10 +104,8 @@ public function reducePoint($point = 1)
105104

106105
/**
107106
* Reset a user point to zero
108-
*
109-
* @return mixed
110107
*/
111-
public function resetPoint(): mixed
108+
public function resetPoint(): static
112109
{
113110
$this->forceFill([$this->getReputationField() => 0])->save();
114111

@@ -117,25 +114,16 @@ public function resetPoint(): mixed
117114

118115
/**
119116
* Get user reputation point
120-
*
121-
* @param bool $formatted
122-
* @return int|string
123117
*/
124118
public function getPoints(bool $formatted = false): int|string
125119
{
126120
$point = $this->{$this->getReputationField()};
127121

128-
if ($formatted) {
129-
return Str::numbers($point);
130-
}
131-
132-
return (int) $point;
122+
return $formatted ? Number::abbreviate($point) : (int) $point;
133123
}
134124

135125
/**
136126
* Get the reputation column name
137-
*
138-
* @return string
139127
*/
140128
protected function getReputationField(): string
141129
{
@@ -146,8 +134,6 @@ protected function getReputationField(): string
146134

147135
/**
148136
* Check for duplicate point allowed
149-
*
150-
* @return bool
151137
*/
152138
protected function isDuplicatePointAllowed(PointType $pointType): bool
153139
{

app-modules/gamify/src/helpers.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
declare(strict_types=1);
44

5+
use App\Models\User;
56
use Laravelcm\Gamify\PointType;
67

78
if (! function_exists('givePoint')) {
89

910
/**
1011
* Give point to user
11-
*
12-
* @param null $payee
1312
*/
14-
function givePoint(PointType $pointType, $payee = null): void
13+
function givePoint(PointType $pointType, ?User $payee = null): void
1514
{
16-
$payee = $payee ?? auth()->user();
15+
$payee ??= auth()->user();
1716

1817
if (! $payee) {
1918
return;
@@ -28,11 +27,12 @@ function givePoint(PointType $pointType, $payee = null): void
2827
/**
2928
* Undo a given point
3029
*
31-
* @param null $payee
30+
* @throws \Laravelcm\Gamify\Exceptions\InvalidPayeeModelException
31+
* @throws \Laravelcm\Gamify\Exceptions\PointSubjectNotSetException
3232
*/
33-
function undoPoint(PointType $pointType, $payee = null): void
33+
function undoPoint(PointType $pointType, ?User $payee = null): void
3434
{
35-
$payee = $payee ?? auth()->user();
35+
$payee ??= auth()->user();
3636

3737
if (! $payee) {
3838
return;

app/Gamify/Points/AddPhone.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace App\Gamify\Points;
66

7-
use Laravelcm\Badges\PointType;
7+
use Laravelcm\Gamify\PointType;
88

99
final class AddPhone extends PointType
1010
{

0 commit comments

Comments
 (0)