Passed
Push — master ( 518ca5...423f94 )
by Oliver
02:46
created

OpeningHoursScopes::scopeOpenAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Webfactor\Laravel\OpeningHours\Traits;
4
5
use Carbon\Carbon;
6
use DateTimeInterface;
7
use Illuminate\Database\Eloquent\Builder;
8
use Webfactor\Laravel\OpeningHours\Entities\Day;
9
use Webfactor\Laravel\OpeningHours\Entities\Time;
10
use Webfactor\Laravel\OpeningHours\Exceptions\InvalidDayName;
11
use Webfactor\Laravel\OpeningHours\Exceptions\InvalidTimeString;
12
13
trait OpeningHoursScopes
14
{
15
    protected $openingHoursRelationName = 'dayOpenTimeRanges';
16
17
    public function scopeWithOpeningHours(Builder $query)
18
    {
19
        return $query->with($this->openingHoursRelationName);
20
    }
21
22
    /**
23
     * Get only open models.
24
     * If no time and day are provided current time and day are used.
25
     * If only time is provided current day is used.
26
     *
27
     * @param Builder $query
28
     * @param Time|string|null $time defaults to current time
29
     * @param string|null $day defaults to current day
30
     * @return Builder|static
0 ignored issues
show
Documentation introduced by
Should the return type not be Builder|\Illuminate\Data...ns\QueriesRelationships?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
31
     * @throws InvalidDayName
32
     * @throws InvalidTimeString
33
     */
34 4 View Code Duplication
    public function scopeOpen(Builder $query, $time = null, string $day = null)
0 ignored issues
show
Duplication introduced by
This method 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...
35
    {
36 4
        $now = Carbon::now();
37 4
        if ($time == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $time of type Webfactor\Laravel\Openin...tities\Time|string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
38 1
            $time = Time::fromDateTime($now);
39 3
        } else if (is_string($time)) {
40 2
            $time = Time::fromString($time);
41
        }
42
43 4
        if ($day == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $day of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
44 2
            $day = Day::onDateTime($now);
45
        } else {
46 2
            if (! Day::isValid($day)) {
47
                throw new InvalidDayName();
48
            }
49
        }
50
51 4
        return $query->whereHas($this->openingHoursRelationName, function(Builder $subquery) use ($day, $time) {
52
            $subquery
53 4
                ->where('day', $day)
54 4
                ->where('start', '<=', $time)
55 4
                ->where('end', '>=', $time);
56 4
        });
57
    }
58
59
    /**
60
     * Get only closed models.
61
     * If no time and day are provided current time and day are used.
62
     * If only time is provided current day is used.
63
     *
64
     * @param Builder $query
65
     * @param Time|string|null $time defaults to current time
66
     * @param string|null $day defaults to current day
67
     * @return Builder|static
0 ignored issues
show
Documentation introduced by
Should the return type not be Builder|\Illuminate\Data...ns\QueriesRelationships?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
68
     * @throws InvalidDayName
69
     * @throws InvalidTimeString
70
     */
71 4 View Code Duplication
    public function scopeClosed(Builder $query, $time = null, string $day = null)
0 ignored issues
show
Duplication introduced by
This method 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...
72
    {
73 4
        $now = Carbon::now();
74 4
        if ($time == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $time of type Webfactor\Laravel\Openin...tities\Time|string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
75 1
            $time = Time::fromDateTime($now);
76 3
        } else if (is_string($time)) {
77 2
            $time = Time::fromString($time);
78
        }
79
80 4
        if ($day == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $day of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
81 2
            $day = Day::onDateTime($now);
82
        } else {
83 2
            if (! Day::isValid($day)) {
84
                throw new InvalidDayName();
85
            }
86
        }
87
88 4
        return $query->whereDoesntHave($this->openingHoursRelationName, function(Builder $subquery) use ($day, $time) {
89
            $subquery
90 4
                ->where('day', $day)
91 4
                ->where('start', '<=', $time)
92 4
                ->where('end', '>=', $time);
93 4
        });
94
    }
95
96
    /**
97
     * Get all models open at the specified day at random time
98
     *
99
     * @param Builder $query
100
     * @param string $day
101
     * @return Builder|static
0 ignored issues
show
Documentation introduced by
Should the return type not be Builder|\Illuminate\Data...ns\QueriesRelationships?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
102
     * @throws InvalidDayName
103
     */
104 1 View Code Duplication
    public function scopeOpenOn(Builder $query, string $day)
0 ignored issues
show
Duplication introduced by
This method 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...
105
    {
106 1
        if (! Day::isValid($day)) {
107
            throw new InvalidDayName();
108
        }
109
110 1
        return $query->whereHas($this->openingHoursRelationName, function (Builder $subquery) use ($day) {
111 1
            $subquery->where('day', $day);
112 1
        });
113
    }
114
115
    /**
116
     * Get all models closed at the specified day
117
     *
118
     * @param Builder $query
119
     * @param string $day
120
     * @return Builder|static
0 ignored issues
show
Documentation introduced by
Should the return type not be Builder|\Illuminate\Data...ns\QueriesRelationships?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
121
     * @throws InvalidDayName
122
     */
123 1 View Code Duplication
    public function scopeClosedOn(Builder $query, string $day)
0 ignored issues
show
Duplication introduced by
This method 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...
124
    {
125 1
        if (! Day::isValid($day)) {
126
            throw new InvalidDayName();
127
        }
128
129 1
        return $query->whereDoesntHave($this->openingHoursRelationName, function (Builder $subquery) use ($day) {
130 1
            $subquery->where('day', $day);
131 1
        });
132
    }
133
134
    /**
135
     * Get all models open at the specified DateTime
136
     *
137
     * @param Builder $query
138
     * @param DateTimeInterface $date
139
     * @return mixed
140
     */
141 1
    public function scopeOpenAt(Builder $query, DateTimeInterface $date)
142
    {
143 1
        return $query->open(Time::fromDateTime($date), Day::onDateTime($date));
144
    }
145
146
    /**
147
     * Get all models closed at the specified DateTime
148
     *
149
     * @param Builder $query
150
     * @param DateTimeInterface $date
151
     * @return mixed
152
     */
153 1
    public function scopeClosedAt(Builder $query, DateTimeInterface $date)
154
    {
155 1
        return $query->closed(Time::fromDateTime($date), Day::onDateTime($date));
156
    }
157
}