Test Failed
Push — master ( fde873...dc1916 )
by vistart
05:54
created

getInvitationRegistrationInvitees()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 2
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\user\models\invitation\registration;
14
15
use rhosocial\base\models\queries\BaseBlameableQuery;
16
use rhosocial\user\models\invitation\Invitation;
17
use rhosocial\user\User;
18
use Yii;
19
use yii\base\InvalidParamException;
20
use yii\db\IntegrityException;
21
22
/**
23
 * Trait UserInvitationRegistrationTrait
24
 * @property-read Invitation[] invitationRegistrations
25
 *
26
 * @package rhosocial\user\models\invitation\registration
27
 * @version 1.0
28
 * @author vistart <[email protected]>
29
 */
30
trait UserInvitationRegistrationTrait
31
{
32
    public $invitationRegistrationClass = Registration::class;
33
    /**
34
     * @param array $associatedModels
35
     * @param array $authRoles
36
     * @param User $inviter
37
     * @return boolean
38
     * @throws \Exception
39
     * @throws InvalidParamException
40
     */
41
    public function registerAccordingToInvitation($associatedModels = [], $authRoles = [], $inviter)
42
    {
43
        if (!$inviter) {
44
            return false;
45
        }
46
        if ($inviter instanceof User && $inviter->getIsNewRecord()) {
47
            throw new InvalidParamException("Inviter cannot be a new user.");
48
        }
49
        $transaction = Yii::$app->db->beginTransaction();
50
        try {
51
            $result = $this->register($associatedModels, $authRoles);
0 ignored issues
show
Bug introduced by
The method register() does not exist on rhosocial\user\models\in...tationRegistrationTrait. Did you maybe mean registerAccordingToInvitation()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
52
            if ($result instanceof \Exception) {
53
                throw $result;
54
            }
55
            if ($result !== true) {
56
                throw new IntegrityException("Registration Failed.");
57
            }
58
            $invitation = $inviter->createInvitationRegistration($this);
59
            $result = $invitation->save();
60
            if (!$result) {
61
                throw new IntegrityException("Record Invitation Failed.");
62
            }
63
            $transaction->commit();
64
        } catch (\Exception $ex) {
65
            $transaction->rollBack();
66
            throw $ex;
67
        }
68
    }
69
70
    /**
71
     * @param $invitee
72
     * @return Registration
73
     */
74
    public function createInvitationRegistration($invitee)
75
    {
76
        return $this->create($this->invitationRegistrationClass, ['invitee' => $invitee]);
0 ignored issues
show
Bug introduced by
The method create() does not exist on rhosocial\user\models\in...tationRegistrationTrait. Did you maybe mean createInvitationRegistration()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
77
    }
78
79
    /**
80
     * Check whether this user enables the invitation from registration feature or not.
81
     * @return boolean
82
     */
83 1
    public function hasEnabledInvitationRegistration()
84
    {
85 1
        if ($this->invitationRegistrationClass === false || !is_string($this->invitationRegistrationClass) || !class_exists($this->invitationRegistrationClass)) {
86
            return false;
87
        }
88 1
        return true;
89
    }
90
91
92
    /**
93
     * @return BaseBlameableQuery
94
     */
95 1
    public function getInvitationRegistrations()
96
    {
97 1
        if (!$this->hasEnabledInvitationRegistration()) {
98
            return null;
99
        }
100 1
        $irClass = $this->invitationRegistrationClass;
101 1
        $noInit = $irClass::buildNoInitModel();
102
        /* @var $noInit Registration */
103 1
        return $this->hasMany($irClass, [$noInit->createdByAttribute => $this->guidAttribute]);
0 ignored issues
show
Bug introduced by
The property guidAttribute does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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...
104
    }
105
106
    /**
107
     * @return User[]
108
     */
109
    public function getInvitationRegistrationInvitees()
110
    {
111
112
    }
113
}
114