MemberTransformer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 1
dl 0
loc 75
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B toFormParamsForCreate() 0 40 7
A toFormParamsForUpdate() 0 20 5
1
<?php
2
3
namespace Mrkj\Laposta\Transformers;
4
5
use Mrkj\Laposta\Models\Member;
6
7
class MemberTransformer
8
{
9
    /**
10
     * @param Member $member
11
     * @param bool $suppressEmailNotification
12
     * @param bool $suppressEmailWelcome
13
     * @param bool $ignoreDoubleOptin
14
     * @return array
15
     */
16
    public static function toFormParamsForCreate(
17
        Member $member,
18
        $suppressEmailNotification = false,
19
        $suppressEmailWelcome = false,
20
        $ignoreDoubleOptin = false
21
    ): array {
22
        $formParams = [
23
            'list_id' => $member->listId,
24
            'ip' => $member->ip,
25
            'email' => $member->email,
26
        ];
27
28
        if ($member->sourceUrl) {
29
            $formParams['source_url'] = $member->sourceUrl;
30
        }
31
32
        if (count($member->getCustomFields()) > 0) {
33
            $formParams['custom_fields'] = $member->getCustomFields();
34
        }
35
36
        $options = [];
37
38
        if ($suppressEmailNotification) {
39
            $options['suppress_email_notification'] = true;
40
        }
41
42
        if ($suppressEmailWelcome) {
43
            $options['suppress_email_welcome'] = true;
44
        }
45
46
        if ($ignoreDoubleOptin) {
47
            $options['ignore_doubleoptin'] = true;
48
        }
49
50
        if (count($options) > 0) {
51
            $formParams['options'] = $options;
52
        }
53
54
        return $formParams;
55
    }
56
57
    /**
58
     * @param Member $member
59
     * @return array
60
     */
61
    public static function toFormParamsForUpdate(Member $member): array
62
    {
63
        $formParams = [
64
            'list_id' => $member->listId,
65
        ];
66
67
        if ($member->email) {
68
            $formParams['email'] = $member->email;
69
        }
70
71
        if ($member->state && in_array($member->state, [Member::STATE_ACTIVE, Member::STATE_UNSUBSCRIBED])) {
72
            $formParams['state'] = $member->state;
73
        }
74
75
        if (count($member->getCustomFields()) > 0) {
76
            $formParams['custom_fields'] = $member->getCustomFields();
77
        }
78
79
        return $formParams;
80
    }
81
}
82