Completed
Push — master ( 5d8ba8...93e296 )
by Scott
02:28
created

DriverConfig   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 13
c 1
b 1
f 0
lcom 1
cbo 2
dl 0
loc 155
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A render() 0 6 1
A prepareVars() 0 4 1
A loadAssets() 0 5 1
A getDriverForm() 0 12 1
A getSaveValue() 0 4 1
A getShippingDrivers() 0 11 2
A onLoadDriverSettings() 0 14 1
A onFormSubmitted() 0 21 3
A save() 0 6 1
1
<?php namespace Bedard\Shop\FormWidgets;
2
3
use Bedard\Shop\Models\DriverConfig as ConfigModel;
4
use Form;
5
use Model;
6
use Backend\Classes\FormWidgetBase;
7
use Bedard\Shop\Classes\DriverManager;
8
use Bedard\Shop\Interfaces\DriverInterface;
9
use October\Rain\Exception\ValidationException;
10
11
/**
12
 * DriverConfig Form Widget.
13
 */
14
class DriverConfig extends FormWidgetBase
15
{
16
    use \Bedard\Shop\Traits\LangJsonable;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected $defaultAlias = 'bedard_shop_driver_config';
22
23
    /**
24
     * @var \Bedard\Shop\Classes\DriverManager;
25
     */
26
    protected $manager;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function init()
32
    {
33
        $this->manager = DriverManager::instance();
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function render()
40
    {
41
        $this->prepareVars();
42
43
        return $this->makePartial('driverconfig');
44
    }
45
46
    /**
47
     * Prepares the form widget view data.
48
     */
49
    public function prepareVars()
50
    {
51
        $this->vars['drivers'] = $this->getShippingDrivers();
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function loadAssets()
58
    {
59
        $this->addJs('/plugins/bedard/shop/assets/dist/vendor.js');
60
        $this->addJs('/plugins/bedard/shop/assets/dist/driverconfig.js', 'Bedard.Shop');
61
    }
62
63
    /**
64
     * Get the form for a driver.
65
     *
66
     * @param  DriverInterface $driver
67
     * @return object
68
     */
69
    protected function getDriverForm(DriverInterface $driver)
70
    {
71
        $model = new Model;
72
        // foreach (array_merge(array_keys($fields), array_keys($tabFields)) as $key) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
        //     $model->$key = $driver->getConfig($key);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
74
        // }
75
76
        $form = $this->makeConfigFromArray($driver->getFormFields());
77
        $form->model = $model;
78
79
        return $form;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getSaveValue($value)
86
    {
87
        return $value;
88
    }
89
90
    /**
91
     * Get the driver details.
92
     *
93
     * @return array
94
     */
95
    protected function getShippingDrivers()
96
    {
97
        foreach ($this->manager->getShippingDrivers() as $driver) {
98
            $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...
99
                'driver' => get_class($driver),
100
                'details' => $driver->driverDetails(),
101
            ];
102
        }
103
104
        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...
105
    }
106
107
    /**
108
     * Load the driver settings.
109
     *
110
     * @return string
111
     */
112
    public function onLoadDriverSettings()
113
    {
114
        $driverClass = input('driver');
115
        $driver = new $driverClass;
116
117
        $config = $this->getDriverForm($driver);
118
        $form = $this->makeWidget('Backend\Widgets\Form', $config);
119
120
        return $this->makePartial('popup', [
121
            'driver' => $driverClass,
122
            'details' => $driver->driverDetails(),
123
            'form' => $form->render(),
124
        ]);
125
    }
126
127
    /**
128
     * Validate the form.
129
     *
130
     * @return void
131
     * @throws \AjaxException
132
     */
133
    public function onFormSubmitted()
134
    {
135
        // grab our relevant input data
136
        $data = input();
137
        $driverClass = $data['_driver'];
138
        $driver = new $driverClass;
139
140
        // clean up fields not needed for driver data
141
        unset($data['_driver']);
142
        unset($data['_session_key']);
143
        unset($data['_token']);
144
145
        // give the driver a change to validate this form if they want to
146
        if (method_exists($driver, 'validate')) $driver->validate($data);
147
148
        // if the driver defined it's own save method, call it
149
        if (method_exists($driver, 'save')) $driver->save($data);
150
151
        // otherwise just use the default save
152
        else $this->save($driverClass, $data);
153
    }
154
155
    /**
156
     * Save a driver.
157
     *
158
     * @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...
159
     * @param  array    $config     Driver data to save.
160
     * @return void
161
     */
162
    protected function save($driverClass, $config)
163
    {
164
        $model = ConfigModel::firstOrNew(['driver' => $driverClass]);
165
        $model->config = $config;
166
        $model->save();
167
    }
168
}
169