Completed
Push — master ( fce117...68ea64 )
by Franco
12s
created

UserInvitation::isExpired()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class UserInvitation
5
 * @package FSWebWorks
6
 * @subpackage UserInvitation
7
 *
8
 * @property string FirstName
9
 * @property string Email
10
 * @property string TempHash
11
 * @property string Groups
12
 * @property int InvitedByID
13
 * @property Member InvitedBy
14
 *
15
 */
16
class UserInvitation extends DataObject
0 ignored issues
show
Bug introduced by
The type DataObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    /**
19
     * Used to control whether a group selection on the invitation form is required.
20
     * @var bool
21
     */
22
    private static $force_require_group = false;
0 ignored issues
show
Unused Code introduced by
The property $force_require_group is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
23
24
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
25
        'FirstName' => 'Varchar',
26
        'Email' => 'Varchar(254)',
27
        'TempHash' => 'Varchar',
28
        'Groups' => 'Text'
29
    );
30
31
    private static $has_one = array(
0 ignored issues
show
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
32
        'InvitedBy' => 'Member'
33
    );
34
35
    private static $indexes = array(
0 ignored issues
show
Unused Code introduced by
The property $indexes is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
36
        'Email' => true,
37
        'TempHash' => true
38
    );
39
40
    /**
41
     * Removes the hash field from the list.
42
     * @return FieldList
0 ignored issues
show
Bug introduced by
The type FieldList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
     */
44
    public function getCMSFields()
45
    {
46
        $fields = parent::getCMSFields();
47
        $fields->removeByName('TempHash');
48
        return $fields;
49
    }
50
51
    public function onBeforeWrite()
52
    {
53
        if (!$this->ID) {
54
            $generator = new RandomGenerator();
0 ignored issues
show
Bug introduced by
The type RandomGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
55
            $this->TempHash = $generator->randomToken('sha1');
56
            $this->InvitedByID = Member::currentUserID();
0 ignored issues
show
Bug introduced by
The type Member was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
        }
58
        parent::onBeforeWrite();
59
    }
60
61
    /**
62
     * Sends an invitation to the desired user
63
     */
64
    public function sendInvitation()
65
    {
66
        return Email::create()
0 ignored issues
show
Bug introduced by
The type Email was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
67
            ->setFrom(Email::config()->get('admin_email'))
68
            ->setTo($this->Email)
69
            ->setSubject(
70
                _t(
0 ignored issues
show
Bug introduced by
The function _t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
                /** @scrutinizer ignore-call */ _t(
Loading history...
71
                    'UserInvation.EMAIL_SUBJECT',
72
                    'Invitation from {name}',
73
                    array('name' => $this->InvitedBy()->FirstName)
74
                )
75
            )->setTemplate('UserInvitationEmail')
76
            ->populateTemplate(
77
                ArrayData::create(
0 ignored issues
show
Bug introduced by
The type ArrayData was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
78
                    array(
79
                        'Invite' => $this,
80
                        'SiteURL' => Director::absoluteBaseURL(),
81
                    )
82
                )
83
            )
84
            ->send();
85
    }
86
87
    /**
88
     * Checks if a user invite was already sent, or if a user is already a member
89
     * @return ValidationResult
0 ignored issues
show
Bug introduced by
The type ValidationResult was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
90
     */
91
    public function validate()
92
    {
93
        $valid = parent::validate();
94
95
        if (self::get()->filter('Email', $this->Email)->first()) {
96
            // UserInvitation already sent
97
            $valid->error(_t('UserInvitation.INVITE_ALREADY_SENT', 'This user was already sent an invite.'));
0 ignored issues
show
Bug introduced by
The function _t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
            $valid->error(/** @scrutinizer ignore-call */ _t('UserInvitation.INVITE_ALREADY_SENT', 'This user was already sent an invite.'));
Loading history...
98
        }
99
100
        if (Member::get()->filter('Email', $this->Email)->first()) {
101
            // Member already exists
102
            $valid->error(_t(
103
                'UserInvitation.MEMBER_ALREADY_EXISTS',
104
                'This person is already a member of this system.'
105
            ));
106
        }
107
        return $valid;
108
    }
109
110
    /**
111
     * Checks if this invitation has expired
112
     * @return bool
113
     */
114
    public function isExpired()
115
    {
116
        $result = false;
117
        $days = self::config()->get('days_to_expiry');
118
        $time = SS_Datetime::now()->Format('U');
0 ignored issues
show
Bug introduced by
The type SS_Datetime was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
119
        $ago = abs($time - strtotime($this->Created));
120
        $rounded = round($ago / 86400);
121
        if ($rounded > $days) {
122
            $result = true;
123
        }
124
        return $result;
125
    }
126
127
    public function canCreate($member = null){
128
        return Permission::check('ACCESS_USER_INVITATIONS');
0 ignored issues
show
Bug introduced by
The type Permission was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
129
    }
130
131
}
132