Collectable::collect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Vetor\Laravel\Collect\Collectable\Models\Traits;
4
5
use DB;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Query\JoinClause;
8
use Vetor\Contracts\Collect\Collection\Models\Collection as CollectionContract;
9
use Vetor\Contracts\Collect\Collectable\Services\CollectableService as CollectableServiceContract;
10
11
trait Collectable
12
{
13
    /**
14
     * @return mixed
15
     */
16 8
    public function collections()
17
    {
18 8
        return $this->morphMany(app(CollectionContract::class), 'collectable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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

18
        return $this->/** @scrutinizer ignore-call */ morphMany(app(CollectionContract::class), 'collectable');
Loading history...
19
    }
20
21
    /**
22
     * @param null $userId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $userId is correct as it would always require null to be passed?
Loading history...
23
     */
24 4
    public function collect($userId = null)
25
    {
26 4
        return app(CollectableServiceContract::class)->addCollectionTo($this, $userId);
0 ignored issues
show
Bug introduced by
$this of type Vetor\Laravel\Collect\Co...dels\Traits\Collectable is incompatible with the type Vetor\Contracts\Collect\...able\Models\Collectable expected by parameter $collectable of Vetor\Contracts\Collect\...vice::addCollectionTo(). ( Ignorable by Annotation )

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

26
        return app(CollectableServiceContract::class)->addCollectionTo(/** @scrutinizer ignore-type */ $this, $userId);
Loading history...
27
    }
28
29
    /**
30
     * @param null $userId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $userId is correct as it would always require null to be passed?
Loading history...
31
     */
32
    public function isCollection($userId = null)
33
    {
34
        return app(CollectableServiceContract::class)->isCollection($this, $userId);
0 ignored issues
show
Bug introduced by
The method isCollection() does not exist on Vetor\Contracts\Collect\...ices\CollectableService. Since it exists in all sub-types, consider adding an abstract or default implementation to Vetor\Contracts\Collect\...ices\CollectableService. ( Ignorable by Annotation )

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

34
        return app(CollectableServiceContract::class)->/** @scrutinizer ignore-call */ isCollection($this, $userId);
Loading history...
35
    }
36
37
    /**
38
     * @param null $userId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $userId is correct as it would always require null to be passed?
Loading history...
39
     */
40 1
    public function cancelCollect($userId = null)
41
    {
42 1
        return app(CollectableServiceContract::class)->removeCollectionFrom($this, $userId);
0 ignored issues
show
Bug introduced by
$this of type Vetor\Laravel\Collect\Co...dels\Traits\Collectable is incompatible with the type Vetor\Contracts\Collect\...able\Models\Collectable expected by parameter $collectable of Vetor\Contracts\Collect\...:removeCollectionFrom(). ( Ignorable by Annotation )

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

42
        return app(CollectableServiceContract::class)->removeCollectionFrom(/** @scrutinizer ignore-type */ $this, $userId);
Loading history...
43
    }
44
45
    /**
46
     * @return int
47
     */
48 1
    public function getCollectionsCountAttribute()
49
    {
50 1
        return $this->collections ? $this->collections->count() : 0;
51
    }
52
53
    /**
54
     * @param \Illuminate\Database\Eloquent\Builder $query
55
     * @param string $direction
56
     * @return mixed
57
     */
58 1
    public function scopeOrderByCollectionsCount(Builder $query, $direction = 'asc')
59
    {
60 1
        $collectable = $query->getModel();
61
62
        return $query
63 1
            ->select("{$collectable->getTable()}.*")
64 1
            ->leftJoin('collections', function (JoinClause $join) use ($collectable) {
65
                $join
66 1
                    ->on('collections.collectable_id', '=', "{$collectable->getTable()}.{$collectable->getKeyName()}")
67 1
                    ->where('collections.collectable_type', '=', $collectable->getMorphClass());
68 1
            })
69 1
            ->groupBy("{$collectable->getTable()}.{$collectable->getKeyName()}")
70 1
            ->orderBy(DB::raw("count(collections.id)"), $direction)
71 1
            ->orderBy("{$collectable->getTable()}.{$collectable->getKeyName()}");
72
    }
73
}
74