TrainingMemberDecorator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 18.46 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 8
dl 12
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTrainingFields() 0 11 1
A getTrainingRequiredFields() 0 11 1
C createOrMerge() 12 28 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 *
4
 * @author nicolaas [at] sunnysideup.co.nz
5
 */
6
class TrainingMemberDecorator extends DataExtension
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
    private static $belongs_many_many = array(
0 ignored issues
show
Unused Code introduced by
The property $belongs_many_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
9
        'Training' => 'TrainingPage'
10
    );
11
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
        'Organisation' => 'Varchar',
13
        'Phone' => 'Varchar'
14
    );
15
16
17
18
    public function getTrainingFields()
19
    {
20
        $fields = new FieldList(
21
            new TextField('FirstName', 'First Name'),
22
            new TextField('Surname', 'Surname'),
23
            new TextField('Organisation'),
24
            new EmailField('Email', 'Email'),
25
            new TextField('Phone')
26
        );
27
        return $fields;
28
    }
29
30
    public function getTrainingRequiredFields()
31
    {
32
        $fields = array(
33
            'FirstName',
34
            'Surname',
35
            'Organisation',
36
            'Email',
37
            'Phone'
38
        );
39
        return $fields;
40
    }
41
42
    public static function createOrMerge($data)
43
    {
44
        // Because we are using a ConfirmedPasswordField, the password will
45
        // be an array of two fields
46 View Code Duplication
        if (isset($data['Password']) && is_array($data['Password'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
            $data['Password'] = $data['Password']['_Password'];
48
        }
49
50
        // We need to ensure that the unique field is never overwritten
51
        $uniqueField = Member::get_unique_identifier_field();
0 ignored issues
show
Deprecated Code introduced by
The method Member::get_unique_identifier_field() has been deprecated with message: 4.0 Use the "Member.unique_identifier_field" config setting instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
52 View Code Duplication
        if (isset($data[$uniqueField])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
            $SQL_unique = Convert::raw2sql($data[$uniqueField]);
54
            $existingUniqueMember = Member::get()->filter(array($uniqueField => $SQL_unique))->first();
55
            if ($existingUniqueMember && $existingUniqueMember->exists()) {
56
                if (Member::currentUserID() != $existingUniqueMember->ID) {
57
                    return false;
58
                }
59
            }
60
        }
61
62
        if (!$member = Member::currentUser()) {
63
            $member = new Member();
64
        }
65
66
        $member->update($data);
67
68
        return $member;
69
    }
70
}
71