Skip to content

Commit 147163a

Browse files
authored
Merge pull request #13 from rennokki/refactor/laravel-6.0-and-refactoring
[refactor] Laravel 6.0 and refactoring the core
2 parents 61324f0 + 7e39a7e commit 147163a

File tree

16 files changed

+114
-46
lines changed

16 files changed

+114
-46
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: php
22

33
php:
4-
- 7.1
54
- 7.2
5+
- 7.3
66

77
env:
88
matrix:

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
}
2323
],
2424
"require": {
25-
"laravel/framework": "~5.5"
25+
"laravel/framework": "~5.5|^6.0"
2626
},
2727
"require-dev": {
28-
"phpunit/phpunit": "^6.2|^7.0",
29-
"orchestra/testbench": "~3.5.0|~3.6.0",
30-
"orchestra/database": "~3.5.0|~3.6.0"
28+
"phpunit/phpunit": "^6.2|^7.0|^8.0",
29+
"orchestra/testbench": "~3.5.0|~3.6.0|~4.0.0",
30+
"orchestra/database": "~3.5.0|~3.6.0|~4.0.0"
3131
},
3232
"autoload": {
3333
"psr-4": {

config/rating.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
*/
88

99
'models' => [
10+
1011
'rating' => \Rennokki\Rating\Models\RaterModel::class,
12+
1113
],
1214

1315
];

database/factories/PageFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
use Illuminate\Support\Str;
4+
25
/*
36
|--------------------------------------------------------------------------
47
| Model Factories
@@ -12,6 +15,6 @@
1215

1316
$factory->define(\Rennokki\Rating\Test\Models\Page::class, function () {
1417
return [
15-
'name' => 'Page'.str_random(5),
18+
'name' => 'Page'.Str::random(5),
1619
];
1720
});

database/factories/SimplePageFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
use Illuminate\Support\Str;
4+
25
/*
36
|--------------------------------------------------------------------------
47
| Model Factories
@@ -12,6 +15,6 @@
1215

1316
$factory->define(\Rennokki\Rating\Test\Models\SimplePage::class, function () {
1417
return [
15-
'name' => 'Page'.str_random(5),
18+
'name' => 'Page'.Str::random(5),
1619
];
1720
});

database/factories/UserFactory.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
use Illuminate\Support\Str;
4+
25
/*
36
|--------------------------------------------------------------------------
47
| Model Factories
@@ -12,9 +15,9 @@
1215

1316
$factory->define(\Rennokki\Rating\Test\Models\User::class, function () {
1417
return [
15-
'name' => 'Name'.str_random(5),
16-
'email' => str_random(5).'@gmail.com',
18+
'name' => 'Name'.Str::random(5),
19+
'email' => Str::random(5).'@gmail.com',
1720
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
18-
'remember_token' => str_random(10),
21+
'remember_token' => Str::random(10),
1922
];
2023
});

database/migrations/2018_07_14_183253_ratings.php

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

3-
use Illuminate\Support\Facades\Schema;
4-
use Illuminate\Database\Schema\Blueprint;
53
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
66

77
class Ratings extends Migration
88
{

src/Contracts/Rater.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,45 @@
44

55
interface Rater
66
{
7+
/**
8+
* Relationship for models that this model currently rated.
9+
*
10+
* @param null|\Illuminate\Database\Eloquent\Model $model
11+
* @return mixed
12+
*/
713
public function ratings($model = null);
814

15+
/**
16+
* Check if the current model is rating another model.
17+
*
18+
* @param \Illuminate\Database\Eloquent\Model $model
19+
* @return bool
20+
*/
921
public function hasRated($model): bool;
1022

11-
public function rate($model, $rating): bool;
23+
/**
24+
* Rate a certain model.
25+
*
26+
* @param \Illuminate\Database\Eloquent\Model $model
27+
* @param float $rating
28+
* @return bool
29+
*/
30+
public function rate($model, float $rating): bool;
1231

13-
public function updateRatingFor($model, $rating): bool;
32+
/**
33+
* Update the rating for a model.
34+
*
35+
* @param \Illuminate\Database\Eloquent\Model $model
36+
* @param float $newRating
37+
* @return bool
38+
*/
39+
public function updateRatingFor($model, $newRating): bool;
1440

41+
/**
42+
* Unrate a certain model.
43+
*
44+
* @param \Illuminate\Database\Eloquent\Model $model
45+
* @return bool
46+
*/
1547
public function unrate($model): bool;
1648
}

src/Models/RaterModel.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,38 @@
66

77
class RaterModel extends Model
88
{
9+
/**
10+
* {@inheritdoc}
11+
*/
912
protected $table = 'ratings';
13+
14+
/**
15+
* {@inheritdoc}
16+
*/
1017
protected $guarded = [];
18+
19+
/**
20+
* {@inheritdoc}
21+
*/
1122
protected $casts = [
1223
'rating' => 'float',
1324
];
1425

26+
/**
27+
* The relationship function for the model who was rated.
28+
*
29+
* @return mixed
30+
*/
1531
public function rateable()
1632
{
1733
return $this->morphTo();
1834
}
1935

36+
/**
37+
* The relationship function for the model who rated.
38+
*
39+
* @return mixed
40+
*/
2041
public function rater()
2142
{
2243
return $this->morphTo();

src/Traits/CanBeRated.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,28 @@ trait CanBeRated
77
/**
88
* Relationship for models that rated this model.
99
*
10-
* @param Model $model The model types of the results.
11-
* @return morphToMany The relationship.
10+
* @param null|\Illuminate\Database\Eloquent\Model $model
11+
* @return mixed
1212
*/
1313
public function raters($model = null)
1414
{
15-
return $this->morphToMany(($model) ?: $this->getMorphClass(), 'rateable', 'ratings', 'rateable_id', 'rater_id')
15+
$modelClass = $model ? (new $model)->getMorphClass() : $this->getMorphClass();
16+
17+
return $this->morphToMany($modelClass, 'rateable', 'ratings', 'rateable_id', 'rater_id')
1618
->withPivot('rater_type', 'rating')
17-
->wherePivot('rater_type', ($model) ?: $this->getMorphClass())
19+
->wherePivot('rater_type', $modelClass)
1820
->wherePivot('rateable_type', $this->getMorphClass());
1921
}
2022

2123
/**
2224
* Calculate the average rating of the current model.
2325
*
24-
* @return float The average rating.
26+
* @param null|\Illuminate\Database\Eloquent\Model $model
27+
* @return float
2528
*/
2629
public function averageRating($model = null): float
2730
{
28-
if ($this->raters($model)->count() == 0) {
31+
if ($this->raters($model)->count() === 0) {
2932
return (float) 0.00;
3033
}
3134

0 commit comments

Comments
 (0)