CollectableService::isCollection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
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();
0 ignored issues
show
Bug introduced by
The method getKey() does not exist on Vetor\Contracts\Collect\Collector\Models\Collector. Since it exists in all sub-types, consider adding an abstract or default implementation to Vetor\Contracts\Collect\Collector\Models\Collector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
            return $userId->/** @scrutinizer ignore-call */ getKey();
Loading history...
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