Completed
Push — master ( 2b6acf...5f05b1 )
by Christopher
03:20
created

PostCategoryScopes::scopeRelatedByPostCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Chriscreate\Blog\Traits\Post;
4
5
use Chriscreate\Blog\Category;
6
use Chriscreate\Blog\Post;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Support\Collection;
9
10
trait PostCategoryScopes
11
{
12
    public function scopeWhereCategories(Builder $query, $categories = null)
13
    {
14
        // search by category name
15
        if (is_string($categories)) {
16
            return $this->whereCategory('name', $categories);
0 ignored issues
show
Bug introduced by
The method whereCategory() does not exist on Chriscreate\Blog\Traits\Post\PostCategoryScopes. Did you maybe mean scopeWhereCategory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
17
        }
18
19
        // search by category id
20
        if (is_int($categories)) {
21
            return $this->whereCategory('id', $categories);
0 ignored issues
show
Bug introduced by
The method whereCategory() does not exist on Chriscreate\Blog\Traits\Post\PostCategoryScopes. Did you maybe mean scopeWhereCategory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
22
        }
23
24
        // search by multiple categories
25
        if (is_array($categories)) {
26
            if (is_int($categories[0])) {
27
                $field = 'id';
28
            } else {
29
                $field = 'name';
30
            }
31
32
            return $this->whereCategory($field, $categories);
0 ignored issues
show
Bug introduced by
The method whereCategory() does not exist on Chriscreate\Blog\Traits\Post\PostCategoryScopes. Did you maybe mean scopeWhereCategory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
33
        }
34
35
        // search by category model
36
        if ($categories instanceof Category) {
37
            return $this->whereCategory('id', $categories->id);
0 ignored issues
show
Bug introduced by
The method whereCategory() does not exist on Chriscreate\Blog\Traits\Post\PostCategoryScopes. Did you maybe mean scopeWhereCategory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
38
        }
39
40
        // search by categories collection
41
        if ($categories instanceof Collection) {
42
            return $this->whereCategory('id', $categories->pluck('id')->toArray());
0 ignored issues
show
Bug introduced by
The method whereCategory() does not exist on Chriscreate\Blog\Traits\Post\PostCategoryScopes. Did you maybe mean scopeWhereCategory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
43
        }
44
45
        return $query;
46
    }
47
48
    public function scopeWhereCategory(Builder $query, ...$options)
49
    {
50
        $collection = collect([
51
            'field' => 'id',
52
            'operator' => '=',
53
            'value' => null,
54
        ]);
55
56
        // Search by field and value
57 View Code Duplication
        if (count($options) == 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
            $collection = $collection->replace(['field' => $options[0]])
59
          ->replace(['value' => $options[1]]);
60
        }
61
62
        // Search by field, operator and value
63 View Code Duplication
        if (count($options) == 3) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
            $collection = $collection->replace(['field' => $options[0]])
65
          ->replace(['operator' => $options[1]])
66
          ->replace(['value' => $options[2]]);
67
        }
68
69
        $query->with('category');
70
71
        if (is_array($collection['value'])) {
72
            return $query->whereHas(
73
              'category',
74
              function (Builder $query) use ($collection) {
75
                  return $query->whereIn(
76
                      $collection['field'],
77
                      $collection['value']
78
                  );
79
              }
80
          );
81
        }
82
83
        return $query->whereHas(
84
          'category',
85
          function (Builder $query) use ($collection) {
86
              return $query->where(
87
                  $collection['field'],
88
                  $collection['operator'],
89
                  $collection['value']
90
              );
91
          }
92
      );
93
    }
94
95
    public function scopeRelatedByPostCategory(Builder $query, Post $post)
96
    {
97
        return $query->whereHas('category', function (Builder $query) use ($post) {
98
            return $query->where('id', $post->category->id);
99
        })->where('id', '!=', $post->id);
100
    }
101
}
102