CreateMemberCommand::getArguments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
use Symfony\Component\Console\Input\InputArgument;
4
use Symfony\Component\Console\Input\InputOption;
5
6
class CreateMemberCommand extends SilverstripeCommand
0 ignored issues
show
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...
7
{
8
    /**
9
     * @var string
10
     */
11
    protected $name = 'security:createmember';
12
13
    /**
14
     * @var string
15
     */
16
    protected $description = 'Create a new Member';
17
18
    public function fire()
19
    {
20
        $member = $this->createMember();
21
22
        if ($member !== false) {
23
            $this->info('Member created with :');
24
            $this->line("ID \t\t: ".$member->ID);
25
            $this->line("Email \t\t: ".$member->Email);
26
            $this->line("Password \t: ".$this->getPasswordFromInputOrEmail($member->Email));
27
            $this->line("FirstName \t: ".$member->FirstName);
28
            if ((bool) $member->Surname) {
29
                $this->line("Surname \t: ".$member->Surname);
30
            }
31
        }
32
    }
33
34
    protected function createMember()
35
    {
36
        $email = $this->getValidEmailInput();
37
        if ($email !== false) {
38
            $member = new Member([
39
                'Email'     => $email,
40
                'Password'  => $this->getPasswordFromInputOrEmail($email),
41
                'FirstName' => $this->getFirstNameFromInputOrEmail($email),
42
                'Surname'   => $this->getLastNameInput(),
43
            ]);
44
45
            $member->write();
46
47
            return $member;
48
        }
49
50
        return false;
51
    }
52
53
    /**
54
     * @param string$email
55
     *
56
     * @return DataObject
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
57
     */
58
    protected function emailExists($email)
59
    {
60
        return (bool) Member::get()->where("Email = '".Convert::raw2sql($email)."'")->first();
61
    }
62
63
    /**
64
     * Get the desired email from the input.
65
     *
66
     * @return bool|string
67
     */
68
    protected function getValidEmailInput()
69
    {
70
        $email = (string) $this->argument('email');
71
72
        if (!Email::is_valid_address($email)) {
73
            $this->error($email.' is not a valid emailaddress');
74
75
            return false;
76
        }
77
78
        if ((bool) $this->emailExists($email)) {
79
            $this->error('A Member already exists with this emailaddress');
80
81
            return false;
82
        }
83
84
        return (string) $email;
85
    }
86
87
    /**
88
     * @param string $email
89
     *
90
     * @return string
91
     */
92
    protected function getPasswordFromInputOrEmail($email)
93
    {
94
        $password = (string) $this->option('password');
95
96
        return (bool) $password ? $password : $email;
97
    }
98
99
    /**
100
     * @param string $email
101
     *
102
     * @return string
103
     */
104
    protected function getFirstNameFromInputOrEmail($email)
105
    {
106
        $firstName = $this->option('firstname');
107
108
        if (!(bool) $firstName) {
109
            list($firstName) = explode('@', $email);
110
        }
111
112
        return (string) $firstName;
113
    }
114
115
    /**
116
     * Surname or Lastname, its the same
117
     * I rather prefer Lastname.
118
     */
119
    protected function getLastNameInput()
120
    {
121
        $surName = (string) $this->option('surname');
122
        $lastName = (string) $this->option('lastname');
123
124
        return (bool) $surName ? $surName : $lastName;
125
    }
126
127
    /**
128
     * @return array
129
     */
130
    protected function getOptions()
131
    {
132
        return [
133
            ['password', 'p', InputOption::VALUE_OPTIONAL, 'Optional Password. Defaults to given emailaddress'],
134
            ['firstname', 'f', InputOption::VALUE_OPTIONAL, 'Optional FirstName. Default to name@ part of the emailaddress'],
135
            ['surname', 's', InputOption::VALUE_OPTIONAL, 'Optional Surname'],
136
            ['lastname', 'l', InputOption::VALUE_OPTIONAL, 'Alias for Surname'],
137
        ];
138
    }
139
140
    /**
141
     * @return array
142
     */
143
    protected function getArguments()
144
    {
145
        return [
146
            ['email', InputArgument::REQUIRED, 'The emailaddress of the Member'],
147
        ];
148
    }
149
}
150