Completed
Push — master ( 510e22...2830aa )
by Carlos
05:38
created

CanFollow::areFollowingEachOther()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
nc 2
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 Illuminate\Support\Facades\DB;
15
use Overtrue\LaravelFollow\Follow;
16
17
/**
18
 * Trait CanFollow.
19
 */
20
trait CanFollow
21
{
22
    /**
23
     * Follow an item or items.
24
     *
25
     * @param int|array|\Illuminate\Database\Eloquent\Model $targets
26
     * @param string                                        $class
27
     *
28
     * @return array
29
     *
30
     * @throws \Exception
31
     */
32
    public function follow($targets, $class = __CLASS__)
33
    {
34
        return Follow::attachRelations($this, 'followings', $targets, $class);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanFollow 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

34
        return Follow::attachRelations(/** @scrutinizer ignore-type */ $this, 'followings', $targets, $class);
Loading history...
35
    }
36
37
    /**
38
     * Unfollow an item or items.
39
     *
40
     * @param int|array|\Illuminate\Database\Eloquent\Model $targets
41
     * @param string                                        $class
42
     *
43
     * @return array
44
     */
45
    public function unfollow($targets, $class = __CLASS__)
46
    {
47
        return Follow::detachRelations($this, 'followings', $targets, $class);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanFollow 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

47
        return Follow::detachRelations(/** @scrutinizer ignore-type */ $this, 'followings', $targets, $class);
Loading history...
48
    }
49
50
    /**
51
     * Toggle follow an item or items.
52
     *
53
     * @param int|array|\Illuminate\Database\Eloquent\Model $targets
54
     * @param string                                        $class
55
     *
56
     * @return array
57
     *
58
     * @throws \Exception
59
     */
60
    public function toggleFollow($targets, $class = __CLASS__)
61
    {
62
        return Follow::toggleRelations($this, 'followings', $targets, $class);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanFollow 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

62
        return Follow::toggleRelations(/** @scrutinizer ignore-type */ $this, 'followings', $targets, $class);
Loading history...
63
    }
64
65
    /**
66
     * Check if user is following given item.
67
     *
68
     * @param int|array|\Illuminate\Database\Eloquent\Model $target
69
     * @param string                                        $class
70
     *
71
     * @return bool
72
     */
73
    public function isFollowing($target, $class = __CLASS__)
74
    {
75
        return Follow::isRelationExists($this, 'followings', $target, $class);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanFollow 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

75
        return Follow::isRelationExists(/** @scrutinizer ignore-type */ $this, 'followings', $target, $class);
Loading history...
76
    }
77
78
    /**
79
     * Check if user and target user is following each other.
80
     *
81
     * @param int|array|\Illuminate\Database\Eloquent\Model $target
82
     * @param string                                        $class
83
     *
84
     * @return bool
85
     */
86
    public function areFollowingEachOther($target, $class = __CLASS__)
87
    {
88
        return Follow::isRelationExists($this, 'followings', $target, $class) && Follow::isRelationExists($target, 'followings', $this, $class);
0 ignored issues
show
Bug introduced by
$this of type Overtrue\LaravelFollow\Traits\CanFollow 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

88
        return Follow::isRelationExists(/** @scrutinizer ignore-type */ $this, 'followings', $target, $class) && Follow::isRelationExists($target, 'followings', $this, $class);
Loading history...
89
    }
90
91
    /**
92
     * Return item followings.
93
     *
94
     * @param string $class
95
     *
96
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
97
     */
98
    public function followings($class = __CLASS__)
99
    {
100
        $table = config('follow.followable_table');
101
        $foreignKey = config('follow.users_table_foreign_key', 'user_id');
102
        $targetTable = (new $class)->getTable();
103
104
        return $this->morphedByMany($class, config('follow.morph_prefix'), $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

104
        return $this->/** @scrutinizer ignore-call */ morphedByMany($class, config('follow.morph_prefix'), $table)
Loading history...
105
                    ->wherePivot('relation', '=', Follow::RELATION_FOLLOW)
106
                    ->withPivot('followable_type', 'relation', 'created_at')
107
                    ->addSelect("{$targetTable}.*", DB::raw("pivot_followables.{$foreignKey} IS NOT NULL AS pivot_each_other"))
108
                    ->leftJoin("{$table} as pivot_followables", function($join) use ($table, $class, $foreignKey) {
109
110
                        $join->on('pivot_followables.followable_type', '=', DB::raw(\addcslashes("'{$class}'", '\\')))
111
                            ->on('pivot_followables.followable_id', '=', "{$table}.{$foreignKey}")
112
                            ->on("pivot_followables.{$foreignKey}", '=', "{$table}.followable_id");
113
                    });
114
    }
115
}
116