Test Failed
Push — master ( 13dd6b...7f7a95 )
by vistart
03:43
created

UserOrganizationTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 119
rs 10
c 1
b 0
f 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getNoInitOrganization() 0 8 2
A getNoInitMember() 0 8 2
A getOfMembers() 0 4 1
A getAtOrganizations() 0 4 1
B setUpOrganization() 0 23 6
A setUpDepartment() 0 4 1
A createOrganization() 0 17 1
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\organization;
14
15
use rhosocial\organization\queries\MemberQuery;
16
use rhosocial\organization\queries\OrganizationQuery;
17
use Yii;
18
use yii\base\InvalidConfigException;
19
20
/**
21
 * @property string $guidAttribute GUID Attribute.
22
 * @property-read Member[] $ofMembers
23
 * @property-read Organization[] $atOrganizations
24
 *
25
 * @version 1.0
26
 * @author vistart <[email protected]>
27
 */
28
trait UserOrganizationTrait
29
{
30
    public $organizationClass = Organization::class;
31
    public $departmentClass = Department::class;
32
    public $memberClass = Member::class;
33
    private $noInitOrganization;
34
    private $noInitMember;
35
    /**
36
     * @return Organization
37
     */
38
    protected function getNoInitOrganization()
39
    {
40
        if (!$this->noInitOrganization) {
41
            $class = $this->organizationClass;
42
            $this->noInitOrganization = $class::buildNoInitModel();
43
        }
44
        return $this->noInitOrganization;
45
    }
46
    /**
47
     * @return Member
48
     */
49
    protected function getNoInitMember()
50
    {
51
        if (!$this->noInitMember) {
52
            $class = $this->memberClass;
53
            $this->noInitMember = $class::buildNoInitModel();
54
        }
55
        return $this->noInitMember;
56
    }
57
58
    /**
59
     * 
60
     * @return MemberQuery
61
     */
62
    public function getOfMembers()
63
    {
64
        return $this->hasMany($this->memberClass, [$this->guidAttribute => $this->getNoInitMember()->memberAttribute])->inverseOf('memberUser');
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
65
    }
66
67
    /**
68
     * 
69
     * @return OrganizationQuery
70
     */
71
    public function getAtOrganizations()
72
    {
73
        return $this->hasMany($this->organizationClass, [$this->guidAttribute => $this->getNoInitOrganization()->guidAttribute])->via('ofMembers');
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
74
    }
75
76
    /**
77
     * Set up organization.
78
     * @param string $name
79
     * @param string $nickname
80
     * @param integer $gravatar_type
81
     * @param string $gravatar
82
     * @param string $timezone
83
     * @param string $description
84
     * @return boolean
85
     */
86
    public function setUpOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '')
0 ignored issues
show
Unused Code introduced by
The parameter $nickname is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $gravatar_type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $gravatar is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $timezone is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $description is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88
        $transaction = Yii::$app->db->beginTransaction();
89
        try {
90
            $models = $this->createOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '');
91
            if (!array_key_exists('organization', $models) || !($models['organization'] instanceof Organization)) {
92
                throw new InvalidConfigException('Invalid Organization Model.');
93
            }
94
            $result = $models['organization']->register($models['associatedModels']);
95
            if ($result instanceof \Exception) {
96
                throw $result;
97
            }
98
            if ($result !== true) {
99
                throw new \Exception('Failed to set up.');
100
            }
101
            $transaction->commit();
102
        } catch (\Exception $ex) {
103
            $transaction->rollBack();
104
            Yii::error($ex->getMessage(), __METHOD__);
105
            return false;
106
        }
107
        return true;
108
    }
109
110
    /**
111
     * Set Up Department.
112
     * @param BaseOrganization $organization
113
     * @param type $department
114
     */
115
    public function setUpDepartment($organization, $department)
0 ignored issues
show
Unused Code introduced by
The parameter $organization is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $department is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
    {
117
        
118
    }
119
120
    /**
121
     * 
122
     * @param string $name
123
     * @param string $nickname
124
     * @param string $gravatar_type
125
     * @param string $gravatar
126
     * @param string $timezone
127
     * @param string $description
128
     */
129
    public function createOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '')
130
    {
131
        $class = $this->organizationClass;
132
        $organization = new $class();
133
        /* @var $organization BaseOrganization */
134
        $profileConfig = [
135
            'name' => $name,
136
            'nickname' => $nickname,
137
            'gravatar_type' => $gravatar_type,
138
            'gravatar' => $gravatar,
139
            'timezone' => $timezone,
140
            'description' => $description,
141
        ];
142
        $profile = $organization->createProfile($profileConfig);
143
        $member = $organization->createMemberModelWithUser($this);
144
        return ['organization' => $organization, 'associatedModels' => ['profile' => $profile, 'creator'=> $member]];
145
    }
146
}
147