CampaignMonitorCustomField   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 2
dl 0
loc 143
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreate() 0 4 1
A canDelete() 0 4 1
A canEdit() 0 4 1
A onBeforeWrite() 0 5 1
A getKey() 0 4 1
A getOptionsAsArray() 0 4 1
A create_from_campaign_monitor_object() 0 23 3
A key_to_code() 0 4 1
B getFormField() 0 21 5
1
<?php
2
3
/**
4
 * @author nicolaas [at] sunnysideup.co.nz
5
 *
6
 * @description: this represents a sub group of a list, otherwise known as a segment
7
 *
8
 **/
9
10
class CampaignMonitorCustomField extends DataObject
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...
11
{
12
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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...
13
        "Code" => "Varchar(64)",
14
        "Title" => "Varchar(64)",
15
        "Type" => "Varchar(32)",
16
        "Options" => "Text",
17
        "Visible" => "Boolean",
18
        "ListID" => "Varchar(32)"
19
    );
20
21
    private static $casting = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $casting 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...
22
        "Key" => "Varchar"
23
    );
24
25
    private static $summary_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $summary_fields 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...
26
        "Title" => "Title",
27
        "Visible.Nice" => "Visible"
28
    );
29
30
    private static $indexes = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $indexes 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...
31
        "ListID" => true,
32
        "Code" => true
33
    );
34
35
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_one 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...
36
        "CampaignMonitorSignupPage" => "CampaignMonitorSignupPage"
37
    );
38
39
    private static $default_sort = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $default_sort 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...
40
        "Visible" => "DESC"
41
    );
42
43
    /**
44
     * form field matcher between CM and SS CMField => SSField
45
     * @return array
46
     */
47
    private static $field_translator = array(
0 ignored issues
show
Unused Code introduced by
The property $field_translator 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...
48
        "MultiSelectOne" => "OptionSetField",
49
        "Text" => "TextField",
50
        "Number" => "NumericField",
51
        "MultiSelectMany" => "CheckboxSetField",
52
        "Date" => "DateField"
53
    );
54
55
56
    public function canCreate($member = null)
57
    {
58
        return false;
59
    }
60
61
    public function canDelete($member = null)
62
    {
63
        return false;
64
    }
65
66
    public function canEdit($member = null)
67
    {
68
        return false;
69
    }
70
71
    public function onBeforeWrite()
72
    {
73
        parent::onBeforeWrite();
74
        $this->Code = self::key_to_code($this->Code);
0 ignored issues
show
Documentation introduced by
The property Code does not exist on object<CampaignMonitorCustomField>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property Code does not exist on object<CampaignMonitorCustomField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
75
    }
76
77
    public function getKey()
78
    {
79
        return "[".$this->Code."]";
0 ignored issues
show
Documentation introduced by
The property Code does not exist on object<CampaignMonitorCustomField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
80
    }
81
82
    public function getOptionsAsArray()
83
    {
84
        return array("" => _t("CampaignMonitor.PLEASE_SELECT", "-- please select --"))+explode(",", $this->Options);
0 ignored issues
show
Bug introduced by
The property Options does not seem to exist. Did you mean create_table_options?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
85
    }
86
87
    /**
88
     *
89
     * @return CampaignMonitorCustomField
90
     */
91
    public static function create_from_campaign_monitor_object($customFieldsObject, $listID)
92
    {
93
        $filterOptions = array(
94
            "ListID" => $listID,
95
            "Code" => self::key_to_code($customFieldsObject->Key)
96
        );
97
        $obj = CampaignMonitorCustomField::get()->filter($filterOptions)->first();
98
        if (!$obj) {
99
            $obj = CampaignMonitorCustomField::create($filterOptions);
100
        }
101
        $page = CampaignMonitorSignupPage::get()->filter(array("ListID" => $listID))->first();
102
        if ($page) {
103
            $obj->CampaignMonitorSignupPageID = $page->ID;
104
        }
105
        $obj->ListID = $listID;
106
        $obj->Code = self::key_to_code($customFieldsObject->Key);
107
        $obj->Title = $customFieldsObject->FieldName;
108
        $obj->Type = $customFieldsObject->DataType;
109
        $obj->Options = implode(",", $customFieldsObject->FieldOptions);
0 ignored issues
show
Bug introduced by
The property Options does not seem to exist. Did you mean create_table_options?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
110
        $obj->Visible = $customFieldsObject->VisibleInPreferenceCenter;
111
        $obj->write();
112
        return $obj;
113
    }
114
115
    private static function key_to_code($key)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
116
    {
117
        return str_replace(array("[", "]"), "", $key);
118
    }
119
120
    /**
121
     * @var array
122
     */
123
    private $_fieldTranslator = array();
124
125
    /**
126
     * @param string $namePrefix
127
     * @param string $nameAppendix
128
     * @param string $title
129
     * @param null | array $options
130
     */
131
    public function getFormField($namePrefix = "", $nameAppendix = "", $title = "", $options = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
132
    {
133
        //sort out names, title
134
        if (!$title) {
135
            $title = $this->Title;
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<CampaignMonitorCustomField>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
136
        }
137
        $name = $namePrefix.$this->Code.$nameAppendix;
0 ignored issues
show
Documentation introduced by
The property Code does not exist on object<CampaignMonitorCustomField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
138
        //create field
139
        if (!count($this->_fieldTranslator)) {
140
            $this->_fieldTranslator = $this->Config()->get("field_translator");
141
        }
142
        $fieldName = $this->_fieldTranslator[$this->Type];
0 ignored issues
show
Documentation introduced by
The property Type does not exist on object<CampaignMonitorCustomField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
143
        $field = $fieldName::create($name, $title);
144
        //add options
145
        if (!$options && $this->Options) {
0 ignored issues
show
Bug introduced by
The property Options does not seem to exist. Did you mean create_table_options?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
146
            $optionsArray = explode(",", $this->Options);
0 ignored issues
show
Bug introduced by
The property Options does not seem to exist. Did you mean create_table_options?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
147
            $optionsArray = array_combine($optionsArray, $optionsArray);
148
            $field->setSource($optionsArray);
149
        }
150
        return $field;
151
    }
152
}
153