1 | <?php |
||
29 | trait UserOrganizationTrait |
||
30 | { |
||
31 | public $organizationClass = Organization::class; |
||
32 | public $memberClass = Member::class; |
||
33 | private $noInitOrganization; |
||
34 | private $noInitMember; |
||
35 | public $lastSetUpOrganization; |
||
36 | /** |
||
37 | * @return Organization |
||
38 | */ |
||
39 | protected function getNoInitOrganization() |
||
40 | { |
||
41 | if (!$this->noInitOrganization) { |
||
42 | $class = $this->organizationClass; |
||
43 | $this->noInitOrganization = $class::buildNoInitModel(); |
||
44 | } |
||
45 | return $this->noInitOrganization; |
||
46 | } |
||
47 | /** |
||
48 | * @return Member |
||
49 | */ |
||
50 | 8 | protected function getNoInitMember() |
|
51 | { |
||
52 | 8 | if (!$this->noInitMember) { |
|
53 | 8 | $class = $this->memberClass; |
|
54 | 8 | $this->noInitMember = $class::buildNoInitModel(); |
|
55 | } |
||
56 | 8 | return $this->noInitMember; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * |
||
61 | * @return MemberQuery |
||
62 | */ |
||
63 | 8 | public function getOfMembers() |
|
67 | |||
68 | /** |
||
69 | * |
||
70 | * @return OrganizationQuery |
||
71 | */ |
||
72 | 8 | public function getAtOrganizations() |
|
76 | |||
77 | /** |
||
78 | * Set up organization. |
||
79 | * @param string $name |
||
80 | * @param Organization $parent |
||
81 | * @param string $nickname |
||
82 | * @param integer $gravatar_type |
||
83 | * @param string $gravatar |
||
84 | * @param string $timezone |
||
85 | * @param string $description |
||
86 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
87 | */ |
||
88 | 17 | public function setUpOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
89 | { |
||
90 | 17 | $transaction = Yii::$app->db->beginTransaction(); |
|
91 | try { |
||
92 | 17 | $models = $this->createOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = ''); |
|
93 | 17 | if (!array_key_exists(0, $models) || !($models[0] instanceof Organization)) { |
|
94 | 1 | throw new InvalidConfigException('Invalid Organization Model.'); |
|
95 | } |
||
96 | 16 | $result = $models[0]->register($models['associatedModels']); |
|
97 | 16 | if ($result instanceof \Exception) { |
|
98 | throw $result; |
||
99 | } |
||
100 | 16 | if ($result !== true) { |
|
101 | throw new \Exception('Failed to set up.'); |
||
102 | } |
||
103 | 16 | if ($parent instanceof Organization && !$parent->getIsNewRecord()) { |
|
104 | 3 | $setParentResult = ($models[0]->setParent($parent) && $models[0]->save()); |
|
105 | } |
||
106 | 16 | if (isset($setParentResult) && $setParentResult === false) { |
|
107 | throw new \Exception('Failed to set parent.'); |
||
108 | } |
||
109 | 16 | $transaction->commit(); |
|
110 | 1 | } catch (\Exception $ex) { |
|
111 | 1 | $transaction->rollBack(); |
|
112 | 1 | Yii::error($ex->getMessage(), __METHOD__); |
|
113 | 1 | throw $ex; |
|
114 | } |
||
115 | 16 | $this->lastSetUpOrganization = $models[0]; |
|
116 | 16 | return true; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Set up organization. |
||
121 | * @param string $name |
||
122 | * @param Organization $parent |
||
123 | * @param string $nickname |
||
124 | * @param integer $gravatar_type |
||
125 | * @param string $gravatar |
||
126 | * @param string $timezone |
||
127 | * @param string $description |
||
128 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
129 | */ |
||
130 | 2 | public function setUpDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
131 | { |
||
132 | 2 | if ($parent == null) { |
|
133 | 1 | throw new InvalidConfigException('Invalid Parent Parameter.'); |
|
134 | } |
||
135 | 1 | $transaction = Yii::$app->db->beginTransaction(); |
|
136 | try { |
||
137 | 1 | $models = $this->createDepartment($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = ''); |
|
138 | 1 | if (!array_key_exists(0, $models) || !($models[0] instanceof Organization)) { |
|
139 | throw new InvalidConfigException('Invalid Organization Model.'); |
||
140 | } |
||
141 | 1 | $result = $models[0]->register($models['associatedModels']); |
|
142 | 1 | if ($result instanceof \Exception) { |
|
143 | throw $result; |
||
144 | } |
||
145 | 1 | if ($result !== true) { |
|
146 | throw new \Exception('Failed to set up.'); |
||
147 | } |
||
148 | 1 | if ($parent instanceof Organization && !$parent->getIsNewRecord()) { |
|
149 | 1 | $setParentResult = ($models[0]->setParent($parent) && $models[0]->save()); |
|
150 | } |
||
151 | 1 | if (isset($setParentResult) && $setParentResult === false) { |
|
152 | throw new \Exception('Failed to set parent.'); |
||
153 | } |
||
154 | 1 | $transaction->commit(); |
|
155 | } catch (\Exception $ex) { |
||
156 | $transaction->rollBack(); |
||
157 | Yii::error($ex->getMessage(), __METHOD__); |
||
158 | throw $ex; |
||
159 | } |
||
160 | 1 | $this->lastSetUpOrganization = $models[0]; |
|
161 | 1 | return true; |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * Create organization. |
||
166 | * @param string $name |
||
167 | * @param string $nickname |
||
168 | * @param string $gravatar_type |
||
169 | * @param string $gravatar |
||
170 | * @param string $timezone |
||
171 | * @param string $description |
||
172 | * @return Organization |
||
173 | */ |
||
174 | 16 | public function createOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
178 | |||
179 | /** |
||
180 | * Create department. |
||
181 | * @param string $name |
||
182 | * @param string $nickname |
||
183 | * @param string $gravatar_type |
||
184 | * @param string $gravatar |
||
185 | * @param string $timezone |
||
186 | * @param string $description |
||
187 | * @return Organization |
||
188 | */ |
||
189 | 1 | public function createDepartment($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
193 | |||
194 | /** |
||
195 | * Create Base Organization. |
||
196 | * @param string $name |
||
197 | * @param string $nickname |
||
198 | * @param integer $gravatar_type |
||
199 | * @param string $gravatar |
||
200 | * @param string $timezone |
||
201 | * @param string $description |
||
202 | * @param integer $type |
||
203 | * @return array This array contains two elements, the first is `Organization` or `Department` depends on `$type`. |
||
204 | * The other is `associatedModels` array, contains two elements `Profile`(profile) and `Creator`(creator). |
||
205 | */ |
||
206 | 16 | protected function createBaseOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '', $type = Organization::TYPE_ORGANIZATION) |
|
223 | } |
||
224 |
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.