|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vetor\Laravel\Collect\Collectable\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Vetor\Contracts\Collect\Collector\Exceptions\InvalidCollector; |
|
6
|
|
|
use Vetor\Contracts\Collect\Collector\Models\Collector as CollectorContract; |
|
7
|
|
|
use Vetor\Contracts\Collect\Collectable\Models\Collectable as CollectableContract; |
|
8
|
|
|
use Vetor\Contracts\Collect\Collectable\Services\CollectableService as CollectableServiceContract; |
|
9
|
|
|
|
|
10
|
|
|
class CollectableService implements CollectableServiceContract |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param \Vetor\Contracts\Collect\Collectable\Models\Collectable $collectable |
|
14
|
|
|
* @param $userId |
|
15
|
|
|
* @return mixed|void |
|
16
|
|
|
*/ |
|
17
|
|
|
public function addCollectionTo(CollectableContract $collectable, $userId) |
|
18
|
|
|
{ |
|
19
|
|
|
$userId = $this->getCollectorUserId($userId); |
|
20
|
|
|
|
|
21
|
|
|
$collection = $collectable->collections()->where('user_id', $userId)->withTrashed()->first(); |
|
22
|
|
|
|
|
23
|
|
|
$collection ? $collection->restore() : $collectable->collections()->create([ |
|
24
|
|
|
'user_id' => $userId, |
|
25
|
|
|
]); |
|
26
|
|
|
|
|
27
|
|
|
return; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param \Vetor\Contracts\Collect\Collectable\Models\Collectable $collectable |
|
32
|
|
|
* @param $userId |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
|
|
public function isCollection(CollectableContract $collectable, $userId) |
|
36
|
|
|
{ |
|
37
|
|
|
$userId = $this->getCollectorUserId($userId); |
|
38
|
|
|
|
|
39
|
|
|
return $collectable->collections()->where('user_id', $userId)->first() ? true : false; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param \Vetor\Contracts\Collect\Collectable\Models\Collectable $collectable |
|
44
|
|
|
* @param $userId |
|
45
|
|
|
* @return mixed|void |
|
46
|
|
|
*/ |
|
47
|
|
|
public function removeCollectionFrom(CollectableContract $collectable, $userId) |
|
48
|
|
|
{ |
|
49
|
|
|
$collection = $collectable->collections()->where('user_id', $this->getCollectorUserId($userId))->first(); |
|
50
|
|
|
|
|
51
|
|
|
$collection && $collection->delete(); |
|
52
|
|
|
|
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param $userId |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getCollectorUserId($userId) |
|
61
|
|
|
{ |
|
62
|
|
|
if ($userId instanceof CollectorContract) { |
|
63
|
|
|
return $userId->getKey(); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (is_null($userId)) { |
|
67
|
|
|
$userId = $this->currentUserId(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if ( !$userId) { |
|
71
|
|
|
throw InvalidCollector::notDefined(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $userId; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function currentUserId() |
|
81
|
|
|
{ |
|
82
|
|
|
return auth()->id(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|