Completed
Push — develop ( c5e17b...b595d4 )
by Nate
04:27
created

AssociateUserToOrganization   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 74
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 17 3
A performAction() 0 4 1
A validBodyParams() 0 4 1
A populate() 0 13 2
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 flipbox\craft\ember\actions\records\SaveRecordTrait;
13
use flipbox\organizations\records\UserAssociation;
14
use yii\base\Action;
15
use yii\db\ActiveRecord;
16
use yii\web\HttpException;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 3.0.0
21
 */
22
class AssociateUserToOrganization extends Action
23
{
24
    use SaveRecordTrait, LookupAssociationTrait;
25
26
    /**
27
     * @var array
28
     */
29
    public $validBodyParams = [
30
        'sortOrder' => 'userOrder',
31
        'state'
32
    ];
33
34
    /**
35
     * @param string $user
36
     * @param string $organization
37
     * @return null|\yii\base\Model|\yii\web\Response
38
     * @throws HttpException
39
     */
40
    public function run(
41
        string $user,
42
        string $organization
43
    )
44
    {
45
        if (null === ($user = $this->findUser($user))) {
46
            return $this->handleNotFoundResponse();
47
        }
48
49
        if (null === ($organization = $this->findOrganization($organization))) {
50
            return $this->handleNotFoundResponse();
51
        }
52
53
        return $this->runInternal(
54
            $organization->getUsers()->findOrCreate($user)
55
        );
56
    }
57
58
    /**
59
     * @inheritdoc
60
     * @param UserAssociation $record
0 ignored issues
show
Bug introduced by
There is no parameter named $record. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
61
     * @return bool
62
     */
63
    protected function performAction(UserAssociation $association): bool
64
    {
65
        return $association->save();
66
    }
67
68
    /**
69
     * Body params that should be set on the record.
70
     *
71
     * @return array
72
     */
73
    protected function validBodyParams(): array
74
    {
75
        return $this->validBodyParams;
76
    }
77
78
    /**
79
     * @param UserAssociation $record
80
     * @inheritDoc
81
     */
82
    protected function populate(UserAssociation $record): ActiveRecord
83
    {
84
        $record->setAttributes(
85
            $this->attributeValuesFromBody()
86
        );
87
88
        $types = Craft::$app->getRequest()->getBodyParam('types', []);
89
        if (!empty($types)) {
90
            $record->getTypes()->clear()->add($types);
91
        }
92
93
        return $record;
94
    }
95
}
96