GeneralController::actionSave()   C
last analyzed

Complexity

Conditions 11
Paths 72

Size

Total Lines 92
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 92
ccs 0
cts 55
cp 0
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 42
nc 72
nop 0
crap 132

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organization\modules\configuration\controllers;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
use flipbox\organization\models\Settings;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
class GeneralController extends AbstractController
20
{
21
22
    /**
23
     * @return \yii\web\Response
24
     */
25
    public function actionSave()
26
    {
27
28
        // Admins and Post requests only
29
        $this->requireAdmin();
30
        $this->requirePostRequest();
31
32
        $request = Craft::$app->getRequest();
33
        $session = Craft::$app->getSession();
34
35
        /** @var Settings $model */
36
        $model = $this->module->module->getSettings();
37
38
        // Statuses from post
39
        if ($rawStatuses = $request->getBodyParam('statuses', [])) {
40
            $statusArray = [];
41
42
            foreach (ArrayHelper::toArray($rawStatuses) as $rawStatus) {
43
                // Make sure we have a label and value
44
                if (empty($rawStatus['label']) || empty($rawStatus['value'])) {
45
                    $model->addError(
46
                        'statuses',
47
                        Craft::t('organization', 'Each status must have a valid label and value.')
48
                    );
49
50
                    break;
51
                }
52
53
                // Add status
54
                $statusArray[$rawStatus['value']] = $rawStatus['label'];
55
            }
56
        }
57
58
        // Set settings array
59
        $model->statuses = !empty($statusArray) ? $statusArray : null;
0 ignored issues
show
Documentation Bug introduced by
It seems like !empty($statusArray) ? $statusArray : null can be null. However, the property $statuses is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
60
61
        // Handle each site's url/template settings
62
        foreach (Craft::$app->getSites()->getAllSites() as $site) {
63
            $namespace = 'sites.' . $site->handle;
64
65
            $postedSettings = $request->getBodyParam($namespace);
66
67
            $siteSettings = $model->getSite($site->id);
68
            $siteSettings->hasUrls = !empty($postedSettings['uriFormat']);
69
70
            if ($siteSettings->hasUrls) {
71
                $siteSettings->uriFormat = $postedSettings['uriFormat'];
72
                $siteSettings->template = $postedSettings['template'];
73
            } else {
74
                $siteSettings->uriFormat = null;
75
                $siteSettings->template = null;
76
            }
77
        }
78
79
        // Save settings
80
        if (!$this->module->getGeneral()->save($model)) {
81
            // Ajax request
82
            if (!$request->getAcceptsJson()) {
83
                // Fail message
84
                $message = Craft::t('organization', 'Settings NOT saved successfully.');
85
86
                // Flash fail message
87
                $session->setError($message);
88
89
                // Send the element back to the template
90
                Craft::$app->getUrlManager()->setRouteParams([
91
                    'settings' => $model
92
                ]);
93
94
                // Redirect
95
                return $this->redirectToPostedUrl($model);
96
            }
97
98
            return $this->asJson([
99
                'success' => false,
100
                'errors' => $model->getErrors(),
101
            ]);
102
        }
103
104
        // Ajax request
105
        if (!$request->getAcceptsJson()) {
106
            // Success message
107
            $message = Craft::t('organization', 'Settings saved successfully.');
108
109
            // Flash success message
110
            $session->setNotice($message);
111
112
            return $this->redirectToPostedUrl($model);
113
        }
114
115
        return $this->asJson($model);
116
    }
117
}
118