OpeningHoursAttribute::parseOpeningHoursForDB()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Webfactor\Laravel\OpeningHours\Traits;
4
5
use Webfactor\Laravel\OpeningHours\Entities\OpeningHours;
6
use Webfactor\Laravel\OpeningHours\Entities\OpeningHoursForDay;
7
use Webfactor\Laravel\OpeningHours\Entities\TimeRange;
8
use Webfactor\Laravel\OpeningHours\Exceptions\Exception;
9
use Webfactor\Laravel\OpeningHours\Models\DayOpenTimeRange;
10
11
trait OpeningHoursAttribute
12
{
13 6
    public function getOpeningHoursAttribute()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
14
    {
15 6
        if (!key_exists('opening_hours', $this->attributes) || !$this->attributes['opening_hours']) {
16 4
            $hours = $this->dayOpenTimeRanges
0 ignored issues
show
Bug introduced by
The property dayOpenTimeRanges does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
                ->groupBy('day')->map(function ($day) {
18 2
                    return $day->map(function (DayOpenTimeRange $range) {
19 2
                        return $range->start.'-'.$range->end;
20 2
                    });
21 4
                });
22
23 4
            $this->attributes['opening_hours'] = OpeningHours::create($hours->toArray());
0 ignored issues
show
Bug introduced by
The property attributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
        }
25
26 6
        return $this->attributes['opening_hours'];
27
    }
28
29 14
    public function setOpeningHoursAttribute($data)
30
    {
31
        // clear previous open times
32 14
        $this->attributes['opening_hours'] = null;
33 14
        $this->dayOpenTimeRanges()->delete();
0 ignored issues
show
Bug introduced by
It seems like dayOpenTimeRanges() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
34
35 14
        if ($data == null) {
36 2
            return;
37
        }
38
39 12
        if ($data instanceof OpeningHours) {
40 1
            $this->applyOpeningHours($data);
41 1
            return;
42
        }
43
44 11
        if (is_array($data) || is_object($data)) {
45 11
            $this->applyOpeningHours(OpeningHours::create((array) $data));
46 11
            return;
47
        }
48
49
        throw new Exception("Invalid argument `{$data}` applied to opening hours attribute.");
50
    }
51
52 12
    private function applyOpeningHours(OpeningHours $openingHours)
53
    {
54 12
        $entries = $this->parseOpeningHoursForDB($openingHours);
55 12
        $this->dayOpenTimeRanges()->saveMany($entries);
0 ignored issues
show
Bug introduced by
It seems like dayOpenTimeRanges() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
56 12
        $this->attributes['opening_hours'] = $openingHours;
57 12
    }
58
59
    private function parseOpeningHoursForDB(OpeningHours $openingHours)
60
    {
61
        $ranges = $openingHours->flatMap(function (OpeningHoursForDay $openingHoursForDay, string $day) {
62 12
            return $openingHoursForDay->map(function (TimeRange $timeRange) use ($day) {
63
                return [
64 12
                    'day'   => $day,
65 12
                    'start' => $timeRange->start(),
66 12
                    'end'   => $timeRange->end()
67
                ];
68 12
            });
69 12
        });
70
71 12
        return collect($ranges)->map(function ($range) {
72 12
            return new DayOpenTimeRange($range);
73 12
        });
74
    }
75
}
76