UserRelationGroupTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
c 2
b 0
f 0
lcom 0
cbo 0
dl 0
loc 39
ccs 5
cts 5
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initUserRelationGroupEvents() 0 4 1
A onDeleteGroup() 0 19 1
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
/**
16
 * 使用此 trait 的类必须与使用 UserRelationTrait 的类配合使用。
17
 * $contentAttribute 关系组名称。
18
 * $contentTypeAttribute 关系组类型。
19
 * @version 2.0
20
 * @author vistart <[email protected]>
21
 */
22
trait UserRelationGroupTrait
23
{
24
25
    public $relationClass;
26
27
    /**
28
     * Attach events associated with user relation group.
29
     */
30 2
    public function initUserRelationGroupEvents()
31
    {
32 2
        $this->on(static::EVENT_BEFORE_DELETE, [$this, 'onDeleteGroup']);
0 ignored issues
show
Bug introduced by
It seems like on() 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...
33 2
    }
34
35
    /**
36
     * the event triggered before deleting group.
37
     * I do not remove group's guid from groupsAttribute which contains the guid
38
     * of group to be deleted.
39
     * @param \yii\base\Event $event
40
     */
41 1
    public function onDeleteGroup($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event 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...
42
    {
43
        /*
44
          $relationClass = $this->relationClass;
45
          if (!is_string($relationClass)) {
46
          throw new \yii\base\NotSupportedException('You must specify the name of relation class.');
47
          }
48
          $sender = $event->sender;
49
          $groupGuid = $sender->guid;
50
          $createdByAttribute = $sender->createdByAttribute;
51
          $relations = $relationClass::findOnesAllRelations($sender->$createdByAttribute);
52
          foreach ($relations as $relation) {
53
          $relation->removeGroup($groupGuid);
54
          if (!$relation->save() && (YII_ENV !== YII_ENV_PROD || YII_DEBUG)) {
55
          $sender->recordWarnings();
56
          }
57
          }
58
         */
59 1
    }
60
}
61