Completed
Pull Request — master (#53)
by
unknown
04:18
created

CanBookmark::bookmark()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the overtrue/laravel-follow
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\LaravelFollow\Traits;
13
14
use Overtrue\LaravelFollow\Follow;
15
16
/**
17
 * Trait CanBookmark.
18
 */
19
trait CanBookmark
20
{
21
    /**
22
     * Follow an item or items.
23
     *
24
     * @param int|array|\Illuminate\Database\Eloquent\Model $targets
25
     * @param string                                        $class
26
     *
27
     * @return array
28
     *
29
     * @throws \Exception
30
     */
31
    public function bookmark($targets, $class = __CLASS__)
32
    {
33
        return Follow::attachRelations($this, 'bookmarks', $targets, $class);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanBookmark is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of Overtrue\LaravelFollow\Follow::attachRelations(). ( Ignorable by Annotation )

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

33
        return Follow::attachRelations(/** @scrutinizer ignore-type */ $this, 'bookmarks', $targets, $class);
Loading history...
34
    }
35
36
    /**
37
     * Unbookmark an item or items.
38
     *
39
     * @param int|array|\Illuminate\Database\Eloquent\Model $targets
40
     * @param string                                        $class
41
     *
42
     * @return array
43
     */
44
    public function unbookmark($targets, $class = __CLASS__)
45
    {
46
        return Follow::detachRelations($this, 'bookmarks', $targets, $class);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanBookmark is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of Overtrue\LaravelFollow\Follow::detachRelations(). ( Ignorable by Annotation )

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

46
        return Follow::detachRelations(/** @scrutinizer ignore-type */ $this, 'bookmarks', $targets, $class);
Loading history...
47
    }
48
49
    /**
50
     * Toggle bookmark an item or items.
51
     *
52
     * @param int|array|\Illuminate\Database\Eloquent\Model $targets
53
     * @param string                                        $class
54
     *
55
     * @return array
56
     *
57
     * @throws \Exception
58
     */
59
    public function toggleBookmark($targets, $class = __CLASS__)
60
    {
61
        return Follow::toggleRelations($this, 'bookmarks', $targets, $class);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanBookmark is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of Overtrue\LaravelFollow\Follow::toggleRelations(). ( Ignorable by Annotation )

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

61
        return Follow::toggleRelations(/** @scrutinizer ignore-type */ $this, 'bookmarks', $targets, $class);
Loading history...
62
    }
63
64
    /**
65
     * Check if user is bookmarked given item.
66
     *
67
     * @param int|array|\Illuminate\Database\Eloquent\Model $target
68
     * @param string                                        $class
69
     *
70
     * @return bool
71
     */
72
    public function hasBookmarked($target, $class = __CLASS__)
73
    {
74
        return Follow::isRelationExists($this, 'bookmarks', $target, $class);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanBookmark is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of Overtrue\LaravelFollow\Follow::isRelationExists(). ( Ignorable by Annotation )

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

74
        return Follow::isRelationExists(/** @scrutinizer ignore-type */ $this, 'bookmarks', $target, $class);
Loading history...
75
    }
76
77
    /**
78
     * Return item bookmarks.
79
     *
80
     * @param string $class
81
     *
82
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
83
     */
84
    public function bookmarks($class = __CLASS__)
85
    {
86
        return $this->morphedByMany($class, config('follow.morph_prefix'), config('follow.followable_table'))
0 ignored issues
show
Bug introduced by
It seems like morphedByMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

86
        return $this->/** @scrutinizer ignore-call */ morphedByMany($class, config('follow.morph_prefix'), config('follow.followable_table'))
Loading history...
87
                    ->wherePivot('relation', '=', Follow::RELATION_BOOKMARK)
88
                    ->withPivot('followable_type', 'relation', 'created_at');
89
    }
90
}
91