Passed
Push — master ( 5e9e3d...710625 )
by Maciej
04:36 queued 11s
created

CampaignCreateHandler::createCampaign()   B

Complexity

Conditions 10
Paths 1

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 22.5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 16
c 1
b 0
f 0
nc 1
nop 10
dl 0
loc 31
ccs 9
cts 18
cp 0.5
crap 22.5
rs 7.6666

How to fix   Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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: CampaignCreateHandler.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\FreshMail\Handler\Campaign;
10
11
use MSlwk\FreshMail\Handler\AbstractHandler;
12
13
/**
14
 * Class CampaignCreateHandler
15
 *
16
 * @package MSlwk\FreshMail\Handler\Campaign
17
 */
18
class CampaignCreateHandler extends AbstractHandler
19
{
20
    const API_ENDPOINT = '/rest/campaigns/create';
21
22
    /**
23
     * @param string $name
24
     * @param string $urlToDownloadContent
25
     * @param string $content
26
     * @param string $subject
27
     * @param string $fromAddress
28
     * @param string $fromName
29
     * @param string $replyTo
30
     * @param string $listHash
31
     * @param string $groupHash
32
     * @param string $resignLink
33
     * @return string
34
     */
35 98
    public function createCampaign(
36
        string $name,
37
        string $urlToDownloadContent = '',
38
        string $content = '',
39
        string $subject = '',
40
        string $fromAddress = '',
41
        string $fromName = '',
42
        string $replyTo = '',
43
        string $listHash = '',
44
        string $groupHash = '',
45
        string $resignLink = ''
46
    ) {
47 98
        $getParameters = [];
48 28
        $postParameters = [
49 98
            'name' => $name,
50
            'url' => $urlToDownloadContent ?: null,
51
            'html' => $content ?: null,
52
            'subject' => $subject ?: null,
53
            'from_address' => $fromAddress ?: null,
54
            'from_name' => $fromName ?: null,
55
            'reply_to' => $replyTo ?: null,
56
            'list' => $listHash ?: null,
57
            'group' => $groupHash ?: null,
58
            'resignlink' => $resignLink ?: null
59
        ];
60
61 70
        $postParameters = array_filter($postParameters, function ($value) {
62 98
            return $value !== null;
63 98
        });
64 98
        $response = $this->processRequest($getParameters, $postParameters);
65 7
        return $response->data->hash;
66
    }
67
68
    /**
69
     * @return string
70
     */
71 7
    protected function getApiEndpoint(): string
72
    {
73 7
        return self::API_ENDPOINT;
74
    }
75
}
76