Completed
Push — master ( 86d7d8...3dc8f7 )
by vistart
04:16
created

UserOrganizationTrait::setUpOrganization()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.5139

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 10
cts 17
cp 0.5881
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 18
nc 8
nop 6
crap 8.5139
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
    public $lastSetUpOrganization;
36
    public $lastSetUpDepartment;
37
    /**
38
     * @return Organization
39
     */
40
    protected function getNoInitOrganization()
41
    {
42
        if (!$this->noInitOrganization) {
43
            $class = $this->organizationClass;
44
            $this->noInitOrganization = $class::buildNoInitModel();
45
        }
46
        return $this->noInitOrganization;
47
    }
48
    /**
49
     * @return Member
50
     */
51 6
    protected function getNoInitMember()
52
    {
53 6
        if (!$this->noInitMember) {
54 6
            $class = $this->memberClass;
55 6
            $this->noInitMember = $class::buildNoInitModel();
56
        }
57 6
        return $this->noInitMember;
58
    }
59
60
    /**
61
     * 
62
     * @return MemberQuery
63
     */
64 6
    public function getOfMembers()
65
    {
66 6
        return $this->hasMany($this->memberClass, [$this->getNoInitMember()->memberAttribute => $this->guidAttribute])->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...
67
    }
68
69
    /**
70
     * 
71
     * @return OrganizationQuery
72
     */
73 6
    public function getAtOrganizations()
74
    {
75 6
        return $this->hasMany($this->organizationClass, [$this->guidAttribute => $this->getNoInitMember()->createdByAttribute])->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...
76
    }
77
78
    /**
79
     * Set up organization.
80
     * @param string $name
81
     * @param string $nickname
82
     * @param integer $gravatar_type
83
     * @param string $gravatar
84
     * @param string $timezone
85
     * @param string $description
86
     * @return boolean
87
     */
88 6
    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...
89
    {
90 6
        $transaction = Yii::$app->db->beginTransaction();
91
        try {
92 6
            $models = $this->createOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '');
93 6
            if (!array_key_exists('organization', $models) || !($models['organization'] instanceof Organization)) {
94
                throw new InvalidConfigException('Invalid Organization Model.');
95
            }
96 6
            $result = $models['organization']->register($models['associatedModels']);
97 6
            if ($result instanceof \Exception) {
98
                throw $result;
99
            }
100 6
            if ($result !== true) {
101
                throw new \Exception('Failed to set up.');
102
            }
103 6
            $transaction->commit();
104
        } catch (\Exception $ex) {
105
            $transaction->rollBack();
106
            Yii::error($ex->getMessage(), __METHOD__);
107
            return false;
108
        }
109 6
        $this->lastSetUpOrganization = $models['organization'];
110 6
        return true;
111
    }
112
113
    /**
114
     * Set Up Department.
115
     * @param BaseOrganization $organization
116
     * @param type $department
117
     */
118
    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...
119
    {
120
        
121
    }
122
123
    /**
124
     * 
125
     * @param string $name
126
     * @param string $nickname
127
     * @param string $gravatar_type
128
     * @param string $gravatar
129
     * @param string $timezone
130
     * @param string $description
131
     */
132 6
    public function createOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '')
133
    {
134 6
        $class = $this->organizationClass;
135 6
        $organization = new $class();
136
        /* @var $organization BaseOrganization */
137
        $profileConfig = [
138 6
            'name' => $name,
139 6
            'nickname' => $nickname,
140 6
            'gravatar_type' => $gravatar_type,
141 6
            'gravatar' => $gravatar,
142 6
            'timezone' => $timezone,
143 6
            'description' => $description,
144
        ];
145 6
        $profile = $organization->createProfile($profileConfig);
146 6
        $member = $organization->createMemberModelWithUser($this);
147 6
        return ['organization' => $organization, 'associatedModels' => ['profile' => $profile, 'creator'=> $member]];
148
    }
149
}
150