Passed
Push — master ( 5ecd82...7f6e89 )
by Davide
30:09
created

EventRepetition::saveWeeklyRepeatDates()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 10
ccs 9
cts 9
cp 1
rs 10
cc 4
nc 4
nop 6
crap 4
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Carbon\Carbon;
7
use Carbon\CarbonInterval;
8
use Carbon\CarbonPeriod;
9
use DavideCasiraghi\LaravelEventsCalendar\Facades\LaravelEventsCalendar;
10
11
class EventRepetition extends Model
12
{
13
    /***************************************************************************/
14
    /**
15
     * The table associated with the model.
16
     *
17
     * @var string
18
     */
19
    protected $table = 'event_repetitions';
20
21
    /***************************************************************************/
22
23
    protected $fillable = [
24
        'event_id', 'start_repeat', 'end_repeat',
25
    ];
26
27
    /*public function user()
28
    {
29
        return $this->belongsTo('DavideCasiraghi\LaravelEventsCalendar\Models\Event', 'event_id', 'id');
30
    }*/
31
32
    /***************************************************************************/
33
34
    /**
35
     * Get for each event the first event repetition in the near future (JUST THE QUERY to use as SUBQUERY).
36
     * Parameters are Start date and End date of the interval
37
     * Return the query string,.
38
     * @param  string $searchStartDate
39
     * @param  string $searchEndDate
40
     * @return string
41
     */
42 5
    public static function getLastestEventsRepetitionsQuery($searchStartDate, $searchEndDate)
43
    {
44
        $ret = self::
45 5
                     selectRaw('event_id, MIN(id) AS rp_id, start_repeat, end_repeat')
46
                     ->when($searchStartDate, function ($query, $searchStartDate) {
47 5
                         return $query->where('event_repetitions.start_repeat', '>=', $searchStartDate);
48 5
                     })
49
                     ->when($searchEndDate, function ($query, $searchEndDate) {
50 1
                         return $query->where('event_repetitions.end_repeat', '<=', $searchEndDate);
51 5
                     })
52 5
                     ->groupBy('event_id');
53
54 5
        return $ret;
55
    }
56
    
57
    /***************************************************************************/
58
59
    /**
60
     * Save event repetition in the DB.
61
     * $dateStart and $dateEnd are in the format Y-m-d
62
     * $timeStart and $timeEnd are in the format H:i:s.
63
     * @param  int $eventId
64
     * @param  string $dateStart
65
     * @param  string $dateEnd
66
     * @param  string $timeStart
67
     * @param  string $timeEnd
68
     * @return void
69
     */
70 10
    public static function saveEventRepetitionOnDB(int $eventId, string $dateStart, string $dateEnd, string $timeStart, string $timeEnd)
71
    {
72 10
        $eventRepetition = new EventRepetition();
73 10
        $eventRepetition->event_id = $eventId;
74
        
75
        //$eventRepetition->start_repeat = $dateStart.' '.$timeStart.':00';
76
        //$eventRepetition->end_repeat = $dateEnd.' '.$timeEnd.':00';
77
        
78
        //create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null
79
        
80
        //$eventRepetition->start_repeat = Carbon::createFromFormat('Y-m-d H:i:s', $dateStart." ".$timeStart);
81
        //$eventRepetition->end_repeat = Carbon::createFromFormat('Y-m-d H:i:s', $dateEnd." ".$timeEnd);
82
        //dump($timeStart);
83 10
        $eventRepetition->start_repeat = Carbon::createFromFormat('Y-m-d H:i', $dateStart." ".$timeStart);
84 10
        $eventRepetition->end_repeat = Carbon::createFromFormat('Y-m-d H:i', $dateEnd." ".$timeEnd);
85
        
86 10
        $eventRepetition->save();
87 10
    }
88
    
89
    /***************************************************************************/
90
91
    /**
92
     * Save all the weekly repetitions in the event_repetitions table.
93
     * $dateStart and $dateEnd are in the format Y-m-d
94
     * $timeStart and $timeEnd are in the format H:i:s.
95
     * $weekDays - $request->get('repeat_weekly_on_day').
96
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Event  $event
97
     * @param  array  $weekDays
98
     * @param  string  $startDate
99
     * @param  string  $repeatUntilDate
100
     * @param  string  $timeStart
101
     * @param  string  $timeEnd
102
     * @return void
103
     */
104 3
    public static function saveWeeklyRepeatDates(int $eventId, array $weekDays, string $startDate, string $repeatUntilDate, string $timeStart, string $timeEnd)
105
    {
106 3
        $beginPeriod = Carbon::createFromFormat('Y-m-d', $startDate);
107 3
        $endPeriod = Carbon::createFromFormat('Y-m-d', $repeatUntilDate);
108 3
        $interval = CarbonInterval::days(1);
0 ignored issues
show
Bug Best Practice introduced by
The method Carbon\CarbonInterval::days() is not static, but was called statically. ( Ignorable by Annotation )

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

108
        /** @scrutinizer ignore-call */ 
109
        $interval = CarbonInterval::days(1);
Loading history...
109 3
        $period = CarbonPeriod::create($beginPeriod, $interval, $endPeriod);
110 3
        foreach ($period as $day) {  // Iterate for each day of the period
111 3
            foreach ($weekDays as $weekDayNumber) { // Iterate for every day of the week (1:Monday, 2:Tuesday, 3:Wednesday ...)
112 3
                if (LaravelEventsCalendar::isWeekDay($day->format('Y-m-d'), $weekDayNumber)) {
0 ignored issues
show
Bug introduced by
The method format() does not exist on null. ( Ignorable by Annotation )

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

112
                if (LaravelEventsCalendar::isWeekDay($day->/** @scrutinizer ignore-call */ format('Y-m-d'), $weekDayNumber)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method isWeekDay() does not exist on DavideCasiraghi\LaravelE...s\LaravelEventsCalendar. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

112
                if (LaravelEventsCalendar::/** @scrutinizer ignore-call */ isWeekDay($day->format('Y-m-d'), $weekDayNumber)) {
Loading history...
113 3
                    EventRepetition::saveEventRepetitionOnDB($eventId, $day->format('Y-m-d'), $day->format('Y-m-d'), $timeStart, $timeEnd);
114
                }
115
            }
116
        }
117 3
    }
118
119
}
120