Completed
Push — master ( b7e188...4523b4 )
by Scott
03:49
created

ShippingSettings::validateDrivers()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.2
cc 4
eloc 7
nc 3
nop 1
1
<?php namespace Bedard\Shop\Models;
2
3
use Flash;
4
use Lang;
5
use Model;
6
use October\Rain\Exception\ValidationException;
7
8
/**
9
 * Cart Settings Model.
10
 */
11
class ShippingSettings extends Model
12
{
13
    /**
14
     * @var array   Behaviors
15
     */
16
    public $implement = ['System.Behaviors.SettingsModel'];
17
18
    /**
19
     * @var string  Settings code
20
     */
21
    public $settingsCode = 'bedard_shop_settings_shipping';
22
23
    /**
24
     * @var string  Settings fields
25
     */
26
    public $settingsFields = 'fields.yaml';
27
28
    /**
29
     * Before save.
30
     *
31
     * @return void
32
     */
33
    public function beforeSave()
34
    {
35
        $this->validateDrivers(self::get('enabled_drivers'));
36
    }
37
38
    /**
39
     * Ensure that only one driver is enabled.
40
     *
41
     * @param   array|null                              $drivers
42
     * @throws  \October\Rain\Database\ModelException
43
     * @return  void
44
     */
45
    protected function validateDrivers(array $drivers = null)
46
    {
47
        if (! $drivers || ! is_array($drivers)) {
48
            Flash::error(Lang::get('bedard.shop::lang.shipping.form.validation_none'));
49
            throw new ValidationException(null);
50
        }
51
52
        // @todo: allow more than one shipping driver to be enabled
53
        if (count($drivers) !== 1) {
54
            Flash::error(Lang::get('bedard.shop::lang.shipping.form.validation_multiple'));
55
            throw new ValidationException(null);
56
        }
57
    }
58
}
59