Completed
Push — master ( 82df8e...bcf569 )
by vistart
74:36 queued 71:28
created

createInvitationRegistration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 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\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
 * @property-read User[] invitationRegistrationInvitees
26
 *
27
 * @package rhosocial\user\models\invitation\registration
28
 * @version 1.0
29
 * @author vistart <[email protected]>
30
 */
31
trait UserInvitationRegistrationTrait
32
{
33
    public $invitationRegistrationClass = Registration::class;
34
    /**
35
     * @param array $associatedModels
36
     * @param array $authRoles
37
     * @param User $inviter
38
     * @return boolean
39
     * @throws \Exception
40
     * @throws InvalidParamException
41
     */
42 6
    public function registerAccordingToInvitation($associatedModels = [], $authRoles = [], $inviter)
43
    {
44 6
        if (!$inviter) {
45
            return false;
46
        }
47 6
        if ($inviter instanceof User && $inviter->getIsNewRecord()) {
48
            throw new InvalidParamException("Inviter cannot be a new user.");
49
        }
50 6
        $transaction = Yii::$app->db->beginTransaction();
51
        try {
52 6
            $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...
53 6
            if ($result instanceof \Exception) {
54
                throw $result;
55
            }
56 6
            if ($result !== true) {
57
                throw new IntegrityException("Registration Failed.");
58
            }
59 6
            $invitation = $inviter->createInvitationRegistration($this);
60 6
            $result = $invitation->save();
61 6
            if (!$result) {
62
                throw new IntegrityException("Record Invitation Failed.");
63
            }
64 6
            $transaction->commit();
65 6
        } catch (\Exception $ex) {
66
            $transaction->rollBack();
67
            throw $ex;
68
        }
69 6
        return true;
70
    }
71
72
    /**
73
     * Create a registration invitation instance.
74
     * @param string|User $invitee The invited person.
75
     * @return Registration
76
     */
77 6
    public function createInvitationRegistration($invitee)
78
    {
79 6
        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...
80
    }
81
82
    /**
83
     * Check whether this user enables the invitation from registration feature or not.
84
     * @return boolean
85
     */
86 7
    public function hasEnabledInvitationRegistration()
87
    {
88 7
        if ($this->invitationRegistrationClass === false || !is_string($this->invitationRegistrationClass) || !class_exists($this->invitationRegistrationClass)) {
89
            return false;
90
        }
91 7
        return true;
92
    }
93
94
    /**
95
     * @return BaseBlameableQuery
96
     */
97 7
    public function getInvitationRegistrations()
98
    {
99 7
        if (!$this->hasEnabledInvitationRegistration()) {
100
            return null;
101
        }
102 7
        $irClass = $this->invitationRegistrationClass;
103 7
        $noInit = $irClass::buildNoInitModel();
104
        /* @var $noInit Registration */
105 7
        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...
106
    }
107
108
    /**
109
     * @return User[]
110
     */
111 1
    public function getInvitationRegistrationInvitees()
112
    {
113 1
        return $this->hasMany(static::class, [$this->guidAttribute => 'invitee_guid'])->via('invitationRegistrations');
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...
114
    }
115
}
116