Completed
Push — master ( 0157fa...b18ad7 )
by Martijn van
02:20
created

CreateMemberCommand::getValidEmailInput()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 9
nc 3
nop 0
1
<?php
2
3
4
use Symfony\Component\Console\Input\InputArgument;
5
use Symfony\Component\Console\Input\InputOption;
6
7
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...
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $name = 'security:createmember';
13
14
    /**
15
     * @var string
16
     */
17
    protected $description = 'Create a new Member';
18
19
    public function fire()
20
    {
21
        $member = $this->createMember();
22
23
        if($member !== false) {
24
            $this->info('Member created with :');
25
            $this->line("ID \t\t: " . $member->ID);
26
            $this->line("Email \t\t: " . $member->Email);
27
            $this->line("Password \t: " . $this->getPasswordFromInputOrEmail($member->Email));
28
            $this->line("FirstName \t: " . $member->FirstName);
29
            if((bool)$member->Surname) {
30
                $this->line("Surname \t: " . $member->Surname);
31
            }
32
        }
33
    }
34
35
    protected function createMember()
36
    {
37
        $email = $this->getValidEmailInput();
38
        if($email !== false) {
39
            $member = new Member([
40
                'Email'     => $email,
41
                'Password'  => $this->getPasswordFromInputOrEmail($email),
42
                'FirstName' => $this->getFirstNameFromInputOrEmail($email),
43
                'Surname'   => $this->getLastNameInput(),
44
            ]);
45
46
            $member->write();
47
48
            return $member;
49
        }
50
        return false;
51
    }
52
53
    /**
54
     * @param string$email
55
     * @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...
56
     */
57
    protected function emailExists($email)
58
    {
59
        return (bool)Member::get()->where("Email = '".Convert::raw2sql($email)."'")->first();
60
    }
61
62
    /**
63
     * Get the desired email from the input.
64
     *
65
     * @return bool|string
66
     */
67
    protected function getValidEmailInput()
68
    {
69
        $email = (string)$this->argument('email');
70
71
        if(!Email::is_valid_address($email)) {
72
            $this->error($email . ' is not a valid emailaddress');
73
            return false;
74
        }
75
76
        if((bool)$this->emailExists($email)) {
77
            $this->error('A Member already exists with this emailaddress');
78
            return false;
79
        }
80
81
        return (string)$email;
82
    }
83
84
    /**
85
     * @param string $email
86
     * @return string
87
     */
88
    protected function getPasswordFromInputOrEmail($email)
89
    {
90
        $password = (string)$this->option('password');
91
        return (bool)$password ? $password : $email;
92
    }
93
94
    /**
95
     * @param string $email
96
     * @return string
97
     */
98
    protected function getFirstNameFromInputOrEmail($email)
99
    {
100
        $firstName = $this->option('firstname');
101
102
        if(!(bool)$firstName) {
103
            list($firstName) = explode('@', $email);
104
        }
105
106
        return (string)$firstName;
107
    }
108
109
    /**
110
     * Surname or Lastname, its the same
111
     * I rather prefer Lastname
112
     */
113
    protected function getLastNameInput()
114
    {
115
        $surName  = (string)$this->option('surname');
116
        $lastName = (string)$this->option('lastname');
117
118
        return (bool)$surName ? $surName : $lastName;
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    protected function getOptions()
125
    {
126
        return [
127
            ['password', 'p', InputOption::VALUE_OPTIONAL, 'Optional Password. Defaults to given emailaddress'],
128
            ['firstname', 'f', InputOption::VALUE_OPTIONAL, 'Optional FirstName. Default to name@ part of the emailaddress'],
129
            ['surname', 's', InputOption::VALUE_OPTIONAL, 'Optional Surname'],
130
            ['lastname', 'l', InputOption::VALUE_OPTIONAL, 'Alias for Surname'],
131
        ];
132
    }
133
134
135
    /**
136
     * @return array
137
     */
138
    protected function getArguments()
139
    {
140
        return [
141
            ['email', InputArgument::REQUIRED, 'The emailaddress of the Member'],
142
        ];
143
    }
144
145
}
146