AccountCreateHandler::registerNewAccount()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2.0008

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 1
b 0
f 0
nc 1
nop 9
dl 0
loc 29
ccs 16
cts 17
cp 0.9412
crap 2.0008
rs 9.7666

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * File: AccountCreateHandler.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\FreshMail\Handler\Account;
10
11
use MSlwk\FreshMail\Handler\AbstractHandler;
12
13
/**
14
 * Class AccountCreateHandler
15
 *
16
 * @package MSlwk\FreshMail\Handler\Account
17
 */
18
class AccountCreateHandler extends AbstractHandler
19
{
20
    const API_ENDPOINT = '/rest/account/create';
21
22
    /**
23
     * @param string $login
24
     * @param string $password
25
     * @param string $firstname
26
     * @param string $lastname
27
     * @param string $phoneNumber
28
     * @param string $company
29
     * @param bool $sendActivationEmail
30
     * @param bool $requireActivation
31
     * @param bool $isChildAccount
32
     * @return \stdClass
33
     */
34 91
    public function registerNewAccount(
35
        string $login,
36
        string $password,
37
        string $firstname,
38
        string $lastname,
39
        string $phoneNumber,
40
        string $company = '',
41
        bool $sendActivationEmail = true,
42
        bool $requireActivation = true,
43
        bool $isChildAccount = false
44
    ): \stdClass {
45 91
        $getParameters = [];
46 26
        $postParameters = [
47 91
            'login' => $login,
48 91
            'password' => $password,
49 91
            'firstname' => $firstname,
50 91
            'lastname' => $lastname,
51 91
            'phone' => $phoneNumber,
52
            'company' => $company ?: null,
53 91
            'activation_mail' => $sendActivationEmail,
54 91
            'activation' => $requireActivation,
55 91
            'child_account' => $isChildAccount
56
        ];
57
58 65
        $postParameters = array_filter($postParameters, function ($value) {
59 91
            return $value !== null;
60 91
        });
61 91
        $response = $this->processRequest($getParameters, $postParameters);
62 7
        return $response->data;
63
    }
64
65
    /**
66
     * @return string
67
     */
68 7
    protected function getApiEndpoint(): string
69
    {
70 7
        return self::API_ENDPOINT;
71
    }
72
}
73