LayoutController::actionSave()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 64
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 64
ccs 0
cts 36
cp 0
rs 8.6346
c 0
b 0
f 0
cc 5
eloc 27
nc 8
nop 0
crap 30

How to fix   Long Method   

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 flipbox\organization\models\Settings;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 1.0.0
17
 */
18
class LayoutController extends AbstractController
19
{
20
21
    /**
22
     * @return \yii\web\Response
23
     */
24
    public function actionSave()
25
    {
26
27
        // Admins and Post requests only
28
        $this->requireAdmin();
29
        $this->requirePostRequest();
30
31
        $request = Craft::$app->getRequest();
32
        $session = Craft::$app->getSession();
33
34
        /** @var Settings $model */
35
        $model = $this->module->module->getSettings();
36
37
        // Handle each site's url/template settings
38
        foreach (Craft::$app->getSites()->getAllSites() as $site) {
39
            $namespace = 'sites.' . $site->handle;
40
41
            $siteSettings = $model->getSite($site->id);
42
            $siteSettings->hasUrls = !empty($postedSettings['uriFormat']);
0 ignored issues
show
Bug introduced by
The variable $postedSettings seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
43
44
            // Group the field layout
45
            $siteSettings->setFieldLayout(
46
                Craft::$app->getFields()->assembleLayoutFromPost($namespace)
47
            );
48
        }
49
50
        // Save settings
51
        if (!$this->module->getLayout()->save($model)) {
52
            // Ajax request
53
            if (!$request->getAcceptsJson()) {
54
                // Fail message
55
                $message = Craft::t('organization', 'Settings NOT saved successfully.');
56
57
                // Flash fail message
58
                $session->setError($message);
59
60
                // Send the element back to the template
61
                Craft::$app->getUrlManager()->setRouteParams([
62
                    'settings' => $model
63
                ]);
64
65
                // Redirect
66
                return $this->redirectToPostedUrl($model);
67
            }
68
69
            return $this->asJson([
70
                'success' => false,
71
                'errors' => $model->getErrors(),
72
            ]);
73
        }
74
75
        // Ajax request
76
        if (!$request->getAcceptsJson()) {
77
            // Success message
78
            $message = Craft::t('organization', 'Settings saved successfully.');
79
80
            // Flash success message
81
            $session->setNotice($message);
82
83
            return $this->redirectToPostedUrl($model);
84
        }
85
86
        return $this->asJson($model);
87
    }
88
}
89