OrganizationController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 135
ccs 0
cts 72
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C actionSave() 0 75 8
B actionDelete() 0 47 4
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\controllers;
10
11
use Craft;
12
use flipbox\organization\elements\Organization as OrganizationElement;
13
use flipbox\organization\Organization as OrganizationPlugin;
14
use yii\web\ForbiddenHttpException;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class OrganizationController extends AbstractController
21
{
22
23
24
    /**
25
     * @return null|\yii\web\Response
26
     * @throws ForbiddenHttpException
27
     */
28
    public function actionSave()
29
    {
30
31
        // POST, PUT, PATCH
32
        $this->requirePostPutPatchRequest();
33
        $this->requireAdmin();
34
35
        $organizationService = OrganizationPlugin::getInstance()->getOrganization();
36
37
        // Organization Id
38
        if ($organizationIdentifier = Craft::$app->getRequest()->getBodyParam('identifier')) {
39
            $organizationElement = $organizationService->get($organizationIdentifier);
40
        } else {
41
            $organizationElement = $organizationService->create();
42
        }
43
44
        /** @var OrganizationElement $organizationElement */
45
        $userElement = Craft::$app->getUser()->getIdentity();
46
47
        if ($organizationElement->getId()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $organizationElement->getId() of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
48
            if (!OrganizationPlugin::getInstance()->getPermission()->canCreateOrganization($userElement)) {
49
                throw new ForbiddenHttpException("You do not have permission to create an organization.");
50
            }
51
        } else {
52
            if (!OrganizationPlugin::getInstance()->getPermission()->canUpdateOrganization(
53
                $userElement,
54
                $organizationElement
55
            )) {
56
                throw new ForbiddenHttpException("You do not have permission to update an organization.");
57
            }
58
        }
59
60
        // Populate element
61
        $organizationService->populateFromRequest($organizationElement);
62
63
        // Save
64
        if (Craft::$app->getElements()->saveElement($organizationElement)) {
65
            // Success message
66
            $message = Craft::t('organization', 'Successfully saved organization.');
67
68
            // Ajax request
69
            if (Craft::$app->getRequest()->isAjax) {
70
                return $this->asJson([
71
                    'success' => true,
72
                    'message' => $message
73
                ]);
74
            }
75
76
            // Flash success message
77
            Craft::$app->getSession()->setNotice($message);
78
79
            // Redirect
80
            return $this->redirectToPostedUrl($organizationElement);
81
        }
82
83
        // Fail message
84
        $message = Craft::t('organization', 'Failed to saved organization.');
85
86
        // Ajax request
87
        if (Craft::$app->getRequest()->isAjax) {
88
            return $this->asErrorJson(
89
                $organizationElement->getErrors()
0 ignored issues
show
Documentation introduced by
$organizationElement->getErrors() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
            );
91
        }
92
93
        // Flash fail message
94
        Craft::$app->getSession()->setError($message);
95
96
        // Send the element back to the template
97
        Craft::$app->getUrlManager()->setRouteParams([
98
            'organization' => $organizationElement
99
        ]);
100
101
        return null;
102
    }
103
104
    /**
105
     * @return \yii\web\Response
106
     */
107
    public function actionDelete()
108
    {
109
110
        // POST, DELETE
111
        $this->requirePostDeleteRequest();
112
113
        // Optional attributes
114
        $organizationId = Craft::$app->getRequest()->getRequiredBodyParam('identifier');
115
116
        /** @var OrganizationElement $organizationElement */
117
        $organizationElement = OrganizationPlugin::getInstance()->getOrganization()->getById($organizationId);
118
119
        // Delete
120
        if (Craft::$app->getElements()->deleteElement($organizationElement)) {
121
            // Success message
122
            $message = Craft::t('organization', 'Successfully deleted organization.');
123
124
            // Ajax request
125
            if (Craft::$app->getRequest()->isAjax) {
126
                return $this->asJson([
127
                    'success' => true,
128
                    'message' => $message
129
                ]);
130
            }
131
132
            // Flash success message
133
            Craft::$app->getSession()->setNotice($message);
134
135
            // Redirect
136
            return $this->redirectToPostedUrl($organizationElement);
137
        }
138
139
        // Fail message
140
        $message = Craft::t('organization', 'Failed to delete organization.');
141
142
        // Ajax request
143
        if (Craft::$app->getRequest()->isAjax) {
144
            return $this->asErrorJson(
145
                $organizationElement->getErrors()
0 ignored issues
show
Documentation introduced by
$organizationElement->getErrors() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
146
            );
147
        }
148
149
        // Flash fail message
150
        Craft::$app->getSession()->setError($message);
151
152
        return null;
153
    }
154
}
155