Completed
Push — master ( 825d81...9c7056 )
by Maxime
08:49
created

ValidatedScope   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 35
loc 35
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 12 12 1
A extend() 6 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Distilleries\Contentful\Models\Scopes;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Scope;
8
use Illuminate\Database\Eloquent\Builder;
9
10 View Code Duplication
class ValidatedScope implements Scope
1 ignored issue
show
Duplication introduced by
This class seems to be duplicated in 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...
11
{
12
    /**
13
     * Apply the scope to a given Eloquent query builder.
14
     *
15
     * @param  \Illuminate\Database\Eloquent\Builder $builder
16
     * @param  \Illuminate\Database\Eloquent\Model $model
17
     * @return void
18
     */
19
    public function apply(Builder $builder, Model $model)
20
    {
21
        $builder
22
            ->where(function ($query) use ($model) {
23
                $query
24
                    ->whereNull($model->getTable() . '.validated_at')
25
                    ->orWhere($model->getTable() . '.validated_at','>=',Carbon::now()->format('Y-m-d H:i:s'));
26
            });
27
28
29
        $this->extend($builder);
30
    }
31
32
    /**
33
     * Extend Builder with following macros.
34
     *
35
     * @param  \Illuminate\Database\Eloquent\Builder $builder
36
     * @return void
37
     */
38
    public function extend(Builder $builder)
39
    {
40
        $builder->macro('withoutValidated', function (Builder $builder) {
41
            return $builder->withoutGlobalScope($this);
42
        });
43
    }
44
}
45