Completed
Push — master ( 636aa8...197f46 )
by Scott
02:31
created

DriverConfig::onFormSubmitted()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 13
nc 4
nop 0
1
<?php namespace Bedard\Shop\FormWidgets;
2
3
use Form;
4
use Model;
5
use Backend\Classes\FormWidgetBase;
6
use Bedard\Shop\Classes\DriverManager;
7
use Bedard\Shop\Interfaces\DriverInterface;
8
use Bedard\Shop\Models\DriverConfig as ConfigModel;
9
10
/**
11
 * DriverConfig Form Widget.
12
 */
13
class DriverConfig extends FormWidgetBase
14
{
15
    use \Bedard\Shop\Traits\LangJsonable;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected $defaultAlias = 'bedard_shop_driver_config';
21
22
    /**
23
     * @var \Bedard\Shop\Classes\DriverManager;
24
     */
25
    protected $manager;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function init()
31
    {
32
        $this->manager = DriverManager::instance();
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function render()
39
    {
40
        $this->prepareVars();
41
42
        return $this->makePartial('driverconfig');
43
    }
44
45
    /**
46
     * Prepares the form widget view data.
47
     */
48
    public function prepareVars()
49
    {
50
        $this->vars['drivers'] = $this->getShippingDrivers();
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function loadAssets()
57
    {
58
        $this->addJs('/plugins/bedard/shop/assets/dist/vendor.js');
59
        $this->addJs('/plugins/bedard/shop/assets/dist/driverconfig.js', 'Bedard.Shop');
60
    }
61
62
    /**
63
     * Get the form for a driver.
64
     *
65
     * @param  DriverInterface $driver
66
     * @return object
67
     */
68
    protected function getDriverForm(DriverInterface $driver, $driverClass)
69
    {
70
        $model = ConfigModel::getDriver($driverClass);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $model is correct as \Bedard\Shop\Models\Driv...getDriver($driverClass) (which targets Bedard\Shop\Models\DriverConfig::getDriver()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
        $form = $this->makeConfigFromArray($driver->getFormFields());
72
        $form->model = $model;
73
74
        return $form;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getSaveValue($value)
81
    {
82
        return $value;
83
    }
84
85
    /**
86
     * Get the driver details.
87
     *
88
     * @return array
89
     */
90
    protected function getShippingDrivers()
91
    {
92
        foreach ($this->manager->getShippingDrivers() as $driver) {
93
            $drivers[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$drivers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $drivers = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
94
                'driver' => get_class($driver),
95
                'details' => $driver->driverDetails(),
96
            ];
97
        }
98
99
        return $drivers;
0 ignored issues
show
Bug introduced by
The variable $drivers does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
100
    }
101
102
    /**
103
     * Load the driver settings.
104
     *
105
     * @return string
106
     */
107
    public function onLoadDriverSettings()
108
    {
109
        $driverClass = input('driver');
110
        $driver = new $driverClass;
111
112
        $config = $this->getDriverForm($driver, $driverClass);
113
        $form = $this->makeWidget('Backend\Widgets\Form', $config);
114
115
        return $this->makePartial('popup', [
116
            'driver' => $driverClass,
117
            'details' => $driver->driverDetails(),
118
            'form' => $form->render(),
119
        ]);
120
    }
121
122
    /**
123
     * Validate the form.
124
     *
125
     * @return void
126
     * @throws \AjaxException
127
     */
128
    public function onFormSubmitted()
129
    {
130
        // grab our relevant input data
131
        $data = input();
132
        $driverClass = $data['_driver'];
133
        $driver = new $driverClass;
134
135
        // clean up fields not needed for driver data
136
        unset($data['_driver']);
137
        unset($data['_session_key']);
138
        unset($data['_token']);
139
140
        // give the driver a change to validate this form if they want to
141
        if (method_exists($driver, 'validate')) {
142
            $driver->validate($data);
143
        }
144
145
        // if the driver defined it's own save method, call it
146
        if (method_exists($driver, 'save')) {
147
            $driver->save($data);
148
        }
149
150
        // otherwise just use the default save
151
        else {
152
            $this->save($driverClass, $data);
153
        }
154
    }
155
156
    /**
157
     * Save a driver.
158
     *
159
     * @param  name     $driver     The class name of the driver.
0 ignored issues
show
Documentation introduced by
There is no parameter named $driver. Did you maybe mean $driverClass?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
160
     * @param  array    $config     Driver data to save.
161
     * @return void
162
     */
163
    protected function save($driverClass, $config)
164
    {
165
        $model = ConfigModel::firstOrNew(['driver' => $driverClass]);
166
        $model->config = $config;
167
        $model->save();
168
    }
169
}
170