1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yoeunes\Commentable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Yoeunes\Commentable\Models\Comment; |
7
|
|
|
use Yoeunes\Commentable\Traits\Commentable; |
8
|
|
|
use Yoeunes\Commentable\Exceptions\EmptyUser; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
10
|
|
|
use Yoeunes\Commentable\Exceptions\UserDoestNotHaveID; |
11
|
|
|
use Yoeunes\Commentable\Exceptions\CommentableModelNotFound; |
12
|
|
|
use Yoeunes\Commentable\Exceptions\ModelDoesNotUseCommentableTrait; |
13
|
|
|
|
14
|
|
|
class CommentBuilder |
15
|
|
|
{ |
16
|
|
|
protected $user; |
17
|
|
|
|
18
|
|
|
protected $commentable; |
19
|
|
|
|
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
if (config('commentable.auth_user')) { |
23
|
|
|
$this->user = auth()->id(); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Model|int $user |
29
|
|
|
* |
30
|
|
|
* @return CommentBuilder |
31
|
|
|
* |
32
|
|
|
* @throws \Throwable |
33
|
|
|
*/ |
34
|
|
|
public function user($user) |
35
|
|
|
{ |
36
|
|
|
throw_if($user instanceof Model && empty($user->id), UserDoestNotHaveID::class, 'User object does not have ID'); |
37
|
|
|
|
38
|
|
|
$this->user = $user instanceof Model ? $user->id : $user; |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Model $commentable |
45
|
|
|
* |
46
|
|
|
* @return CommentBuilder |
47
|
|
|
* |
48
|
|
|
* @throws \Throwable |
49
|
|
|
*/ |
50
|
|
|
public function commentable(Model $commentable) |
51
|
|
|
{ |
52
|
|
|
throw_unless(in_array(Commentable::class, class_uses_recursive($commentable)), ModelDoesNotUseCommentableTrait::class, get_class($commentable).' does not use the Commentable Trait'); |
53
|
|
|
|
54
|
|
|
$this->commentable = $commentable; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $body |
61
|
|
|
* |
62
|
|
|
* @return Comment |
63
|
|
|
* |
64
|
|
|
* @throws \Throwable |
65
|
|
|
*/ |
66
|
|
|
public function comment(string $body) |
67
|
|
|
{ |
68
|
|
|
throw_if(empty($this->user), EmptyUser::class, 'Empty user'); |
69
|
|
|
|
70
|
|
|
throw_if(empty($this->commentable->id), CommentableModelNotFound::class, 'Commentable model not found'); |
71
|
|
|
|
72
|
|
|
$data = [ |
73
|
|
|
'user_id' => $this->user, |
74
|
|
|
'commentable_id' => $this->commentable->id, |
75
|
|
|
'commentable_type' => in_array(get_class($this->commentable), Relation::morphMap()) ? array_search(get_class($this->commentable), Relation::morphMap()) : get_class($this->commentable), |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$commentModel = config('commentable.comment'); |
79
|
|
|
|
80
|
|
|
$comment = (new $commentModel)->fill($data); |
81
|
|
|
|
82
|
|
|
$comment->comment = $body; |
83
|
|
|
|
84
|
|
|
$comment->save(); |
85
|
|
|
|
86
|
|
|
return $comment; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: