Completed
Push — master ( 20c7c7...fa6eea )
by Scott
02:26
created

StartEndable::bootStartEndable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php namespace Bedard\Shop\Traits;
2
3
use Flash;
4
use Lang;
5
use October\Rain\Database\ModelException;
6
7
trait StartEndable
8
{
9
    /**
10
     * Boot the startendable trait for this model.
11
     *
12
     * @return void
13
     */
14
    public static function bootStartEndable()
15
    {
16
        static::extend(function($model) {
17
            $model->bindEvent('model.afterValidate', function() use ($model) {
18
                $model->validateStartEndDates();
19
            });
20
        });
21
    }
22
23
    /**
24
     * Ensure the start and end dates are valid.
25
     *
26
     * @return void
27
     */
28
    public function validateStartEndDates()
29
    {
30
        // Start date must be after the end date
31
        if ($this->start_at !== null &&
0 ignored issues
show
Bug introduced by
The property start_at 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...
32
            $this->end_at !== null &&
0 ignored issues
show
Bug introduced by
The property end_at 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...
33
            $this->start_at >= $this->end_at) {
34
            Flash::error(Lang::get('bedard.shop::lang.traits.startendable.start_at_invalid'));
35
            throw new ModelException($this);
36
        }
37
    }
38
}
39