Completed
Push — ft/remove-vendor ( 825fc6...4801b9 )
by Philippe
56:00 queued 47:37
created

HasPeriodTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 57.14%

Importance

Changes 0
Metric Value
wmc 8
eloc 12
c 0
b 0
f 0
dl 0
loc 40
ccs 12
cts 21
cp 0.5714
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A bootHasPeriodTrait() 0 3 1
A initializeHasPeriodTrait() 0 3 1
A scopePassed() 0 3 1
A scopeUpcoming() 0 3 1
A saveStartAtField() 0 6 1
A scopeOngoing() 0 3 1
A saveEndAtField() 0 5 2
1
<?php
2
3
namespace Thinktomorrow\Chief\Concerns\HasPeriod;
4
5
use Illuminate\Support\Carbon;
6
7
trait HasPeriodTrait
8
{
9 8
    protected static function bootHasPeriodTrait()
10
    {
11 8
        static::addGlobalScope(new SortPeriodDateScope);
12 8
    }
13
14 8
    public function initializeHasPeriodTrait()
15
    {
16 8
        $this->dates = array_merge($this->dates, ['start_at', 'end_at']);
0 ignored issues
show
Bug Best Practice introduced by
The property dates does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17 8
    }
18
19 1
    public function scopePassed($query)
20
    {
21 1
        return $query->where('end_at', '<', Carbon::now());
22
    }
23
24 1
    public function scopeUpcoming($query)
25
    {
26 1
        return $query->where('start_at', '>', Carbon::now());
27
    }
28
29 1
    public function scopeOngoing($query)
30
    {
31 1
        return $query->where('start_at', '<', Carbon::now())->where('end_at', '>', Carbon::now());
32
    }
33
34
    public function saveStartAtField($start_at)
35
    {
36
        $this->start_at = Carbon::parse($start_at)->startOfDay();
0 ignored issues
show
Bug Best Practice introduced by
The property start_at does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
        $this->end_at   = $this->start_at->endOfDay();
0 ignored issues
show
Bug Best Practice introduced by
The property end_at does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
39
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() 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

39
        $this->/** @scrutinizer ignore-call */ 
40
               save();
Loading history...
40
    }
41
42
    public function saveEndAtField($end_at)
43
    {
44
        if ($end_at) {
45
            $this->end_at = Carbon::parse($end_at)->endOfDay();
0 ignored issues
show
Bug Best Practice introduced by
The property end_at does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
            $this->save();
47
        }
48
    }
49
}
50