Completed
Push — master ( b27204...a2c64d )
by Nate
05:15 queued 02:48
created

organizations/PopulateOrganizationTrait.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 \flipbox\craft\ember\exceptions\RecordNotFoundException
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
        // Active type
76
        $type = Craft::$app->getRequest()->getParam('type');
77
        if (!empty($type)) {
78
            $organization->setActiveType(
79
                OrganizationType::getOne($type)
0 ignored issues
show
\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...
80
            );
81
        }
82
83
        // Set types
84
        $organization->setTypesFromRequest(
85
            (string)$request->getParam('typesLocation', 'types')
86
        );
87
88
        // Set users
89
        $organization->setUsersFromRequest(
90
            (string)$request->getParam('usersLocation', 'users')
91
        );
92
93
        // Set content
94
        $organization->setFieldValuesFromRequest(
95
            (string)$request->getParam('fieldsLocation', 'fields')
96
        );
97
    }
98
99
    /**
100
     * @param OrganizationElement $organization
101
     * @return OrganizationElement
102
     */
103
    protected function populateSiteFromRequest(OrganizationElement $organization)
104
    {
105
        if ($site = $this->resolveSiteFromRequest()) {
106
            $organization->siteId = $site->id;
107
        }
108
        return $organization;
109
    }
110
111
    /**
112
     * @param OrganizationElement $organization
113
     * @param string $dateProperty
114
     */
115
    private function populateDateFromRequest(OrganizationElement $organization, string $dateProperty)
116
    {
117
        $dateTime = DateTimeHelper::toDateTime(
118
            Craft::$app->getRequest()->getBodyParam($dateProperty, $organization->{$dateProperty})
119
        );
120
        $organization->{$dateProperty} = $dateTime === false ? null : $dateTime;
121
    }
122
123
    /*******************************************
124
     * RESOLVE SITE
125
     *******************************************/
126
127
    /**
128
     * @return Site|null
129
     */
130
    protected function resolveSiteFromRequest()
131
    {
132
        if (!$site = Craft::$app->getRequest()->getParam('site')) {
133
            $site = Craft::$app->getSites()->currentSite;
134
        }
135
136
        return SiteHelper::resolve($site);
137
    }
138
}
139