Completed
Push — develop ( 9b3aa8...05d944 )
by Nate
06:53
created

populateExtraValuesFromRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 2
cp 0
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
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\organizations\actions\organizations;
10
11
use Craft;
12
use craft\helpers\DateTimeHelper;
13
use craft\models\Site;
14
use flipbox\craft\ember\helpers\SiteHelper;
15
use flipbox\organizations\elements\Organization as OrganizationElement;
16
use flipbox\organizations\records\OrganizationType;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
trait PopulateOrganizationTrait
23
{
24
25
    /**
26
     * @return array
27
     */
28
    protected function validBodyParams(): array
29
    {
30
        return [];
31
    }
32
33
    /*******************************************
34
     * POPULATE (from Request)
35
     *******************************************/
36
37
    /**
38
     * @param OrganizationElement $organization
39
     * @throws \Exception
40
     */
41
    protected function populateFromRequest(OrganizationElement $organization)
42
    {
43
        $request = Craft::$app->getRequest();
44
45
        // Title
46
        $organization->title = (string)$request->getBodyParam(
47
            'title',
48
            $organization->title
49
        );
50
51
        // Slug
52
        $organization->slug = (string)$request->getBodyParam(
53
            'slug',
54
            $organization->slug
55
        );
56
57
        // Enabled
58
        $organization->enabled = (bool)$request->getBodyParam(
59
            'enabled',
60
            $organization->enabled
61
        );
62
63
        // Enabled for Site
64
        $organization->enabledForSite = (bool)$request->getBodyParam(
65
            'enabledForSite',
66
            $organization->enabledForSite
67
        );
68
69
        // Site
70
        $this->populateSiteFromRequest($organization);
71
72
        // Join date
73
        $this->populateDateFromRequest($organization, 'dateJoined');
74
75
        // Set types
76
        $organization->setTypesFromRequest(
77
            (string)$request->getParam('typesLocation', 'types')
78
        );
79
80
        // Set users
81
        $organization->setUsersFromRequest(
82
            (string)$request->getParam('usersLocation', 'users')
83
        );
84
85
        // Set content
86
        $organization->setFieldValuesFromRequest(
87
            (string)$request->getParam('fieldsLocation', 'fields')
88
        );
89
90
        // Extra content
91
        $this->populateExtraValuesFromRequest(
92
            $organization,
93
            (string)$request->getParam('extraLocation', 'extra')
94
        );
95
96
        // Active type
97
        $type = Craft::$app->getRequest()->getParam('type');
98
        if (!empty($type)) {
99
            $organization->setActiveType(
100
                OrganizationType::getOne($type)
0 ignored issues
show
Documentation introduced by
\flipbox\organizations\r...tionType::getOne($type) is of type object<yii\db\ActiveRecordInterface>|array, but the function expects a null|object<flipbox\orga...cords\OrganizationType>.

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...
101
            );
102
        }
103
    }
104
105
    /**
106
     * @param OrganizationElement $organization
107
     * @return OrganizationElement
108
     */
109
    protected function populateSiteFromRequest(OrganizationElement $organization)
110
    {
111
        if ($site = $this->resolveSiteFromRequest()) {
112
            $organization->siteId = $site->id;
113
        }
114
        return $organization;
115
    }
116
117
    /**
118
     * @param OrganizationElement $organization
119
     * @param string $dateProperty
120
     * @throws \Exception
121
     */
122
    private function populateDateFromRequest(OrganizationElement $organization, string $dateProperty)
123
    {
124
        $dateTime = DateTimeHelper::toDateTime(
125
            Craft::$app->getRequest()->getBodyParam($dateProperty, $organization->{$dateProperty})
126
        );
127
        $organization->{$dateProperty} = $dateTime === false ? null : $dateTime;
128
    }
129
130
    /**
131
     * Populate any 'extra' namespaced input values on the organization
132
     *
133
     * @param OrganizationElement $organization
134
     * @param string $identifier
135
     */
136
    public function populateExtraValuesFromRequest(OrganizationElement $organization, string $identifier = 'extra')
137
    {
138
        if (null !== ($data = Craft::$app->getRequest()->getBodyParam($identifier))) {
139
            if (!is_array($data)) {
140
                return;
141
            }
142
143
            Craft::configure(
144
                $organization,
145
                $data
146
            );
147
        }
148
    }
149
150
    /*******************************************
151
     * RESOLVE SITE
152
     *******************************************/
153
154
    /**
155
     * @return Site|null
156
     */
157
    protected function resolveSiteFromRequest()
158
    {
159
        if (!$site = Craft::$app->getRequest()->getParam('site')) {
160
            $site = Craft::$app->getSites()->currentSite;
161
        }
162
163
        return SiteHelper::resolve($site);
164
    }
165
}
166