ModuleProductEmail_Form::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 13
nc 1
nop 3
1
<?php
2
3
/**
4
 * @author nicolaas [at] sunny side up . co . nz
5
 * this extension of product is for software products (modules)
6
 *
7
 *
8
 **/
9
10
11
class ModuleProductEmail 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...
12
{
13
    private static $icon = "ecommerce_software/images/treeicons/ModuleProduct";
0 ignored issues
show
Unused Code introduced by
The property $icon 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...
14
15
    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...
16
        "Subject" => "Varchar",
17
        "Body" => "HTMLText",
18
        "To" => "Varchar(255)",
19
        "Sent" => "Boolean"
20
    );
21
22
    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...
23
        "ModuleProduct" => "ModuleProduct",
24
        "Member" => "Member"
25
    );
26
27
    private static $singular_name = "Module Email";
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 $singular_name 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...
28
    public function i18n_singular_name()
29
    {
30
        return _t("ModuleProductEmail.MODULEPRODUCTEMAIL", "Module Email");
31
    }
32
33
    private static $plural_name = "Module Emails";
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 $plural_name 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...
34
    public function i18n_plural_name()
35
    {
36
        return _t("ModuleProductEmail.MODULEPRODUCTEMAILS", "Module Emails");
37
    }
38
39
    public function canDelete($member = null)
40
    {
41
        return false;
42
    }
43
44
    public function canEdit($member = null)
45
    {
46
        return false;
47
    }
48
49
    public function onAfterWrite()
50
    {
51
        parent::onAfterWrite();
52
        if (!$this->Sent) {
0 ignored issues
show
Documentation introduced by
The property Sent does not exist on object<ModuleProductEmail>. 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...
53
            $email = new Email(
54
                $from = Email::getAdminEmail(),
0 ignored issues
show
Deprecated Code introduced by
The method Email::getAdminEmail() has been deprecated with message: 4.0 Use the "Email.admin_email" 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...
55
                $to = $this->To,
0 ignored issues
show
Documentation introduced by
The property To does not exist on object<ModuleProductEmail>. 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...
56
                $subject = $this->Subject,
0 ignored issues
show
Documentation introduced by
The property Subject does not exist on object<ModuleProductEmail>. 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...
57
                $body = $this->Body,
0 ignored issues
show
Documentation introduced by
The property Body does not exist on object<ModuleProductEmail>. 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...
58
                $bounceHandlerURL = null,
59
                $cc = null,
60
                $bcc = Email::getAdminEmail()
0 ignored issues
show
Deprecated Code introduced by
The method Email::getAdminEmail() has been deprecated with message: 4.0 Use the "Email.admin_email" 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...
61
            );
62
            $email->send();
63
            $this->Sent = 1;
0 ignored issues
show
Documentation introduced by
The property Sent does not exist on object<ModuleProductEmail>. 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...
64
            $this->write();
65
        }
66
    }
67
}
68
69
70
class ModuleProductEmail_Form extends Form
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
71
{
72
    public function __construct($controller, $name, $moduleProduct)
73
    {
74
        $defaults = $moduleProduct->EmailDefaults();
75
        $fields = new FieldList();
76
        $fields->push(new TextField('To', 'To', $defaults->To));
77
        $fields->push(new TextField('Subject', 'Subject', $defaults->Subject));
78
        $fields->push(new HiddenField('ModuleProductID', 'ModuleProductID', $moduleProduct->ID));
79
        $fields->push(new HiddenField('MemberID', 'memberID', $moduleProduct->DefaultMemberID()));
80
        $fields->push(new HtmlEditorField('Body', 'Body', $defaults->Body));
81
        $actions = new FieldList(new FormAction("submit", "submit"));
82
        $validator = new ModuleProductEmail_RequiredFields(array("Subject"));
83
        parent::__construct($controller, $name, $fields, $actions, $validator);
84
        $this->loadDataFrom($moduleProduct->emailDefaults());
85
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
86
    }
87
88
    public function submit($data, $form)
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...
89
    {
90
        $member = Member::currentUser();
91
        if (!$member || !$member->inGroup("ADMIN")) {
92
            $form->setMessage("You need to be logged as an admin to send this email.", "bad");
93
            return Controller::curr()->redirectBack();
94
        }
95
        $data = Convert::raw2sql($data);
96
        $page = null;
97
        if (isset($data["ModuleProductID"])) {
98
            $page = ModuleProduct::get()->byID(intval($data["ModuleProductID"]));
99
        }
100
        if (!$page) {
101
            $form->setMessage("Can not find the right page for saving this email.", "bad");
102
            return Controller::curr()->redirectBack();
103
        }
104
        $email = new ModuleProductEmail();
105
        $form->saveInto($email);
106
        $email->write();
107
        if (Director::is_ajax()) {
108
            return "mail sent!";
109
        } else {
110
            return Controller::curr()->redirect($page->Link());
111
        }
112
    }
113
}
114
115
class ModuleProductEmail_RequiredFields extends RequiredFields
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
116
{
117
    public function __construct($array)
118
    {
119
        parent::__construct($array);
120
    }
121
122
    public function javascript()
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...
123
    {
124
        $js = '';
125
        Requirements::customScript($js, "ModuleProductEmail");
126
        return parent::javascript();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class RequiredFields as the method javascript() does only exist in the following sub-classes of RequiredFields: AddingModuleProduct_RequiredFields, ModuleProductEmail_RequiredFields. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
127
    }
128
129
130
    /**
131
    * Allows validation of fields via specification of a php function for validation which is executed after
132
    * the form is submitted
133
    */
134
    public function php($data)
135
    {
136
        $valid = true;
137 View Code Duplication
        if (!isset($data["Subject"]) || (isset($data["Subject"]) && strlen($data["Subject"])) < 3) {
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...
138
            $errorMessage = _t("Form.PLEASEENTERASUBJECT", "Please enter a subject");
139
            $this->validationError(
140
                $fieldName = "Subject",
141
                $errorMessage,
142
                "required"
143
            );
144
            $valid = false;
145
        }
146 View Code Duplication
        if (!isset($data["To"]) || (isset($data["To"]) && strlen($data["To"]) < 3)) {
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...
147
            $errorMessage = _t("Form.PLEASEENTERANEMAIL", "Please enter an e=mail");
148
            $this->validationError(
149
                $fieldName = "To",
150
                $errorMessage,
151
                "required"
152
            );
153
        }
154
        if (!$valid) {
155
            return false;
156
        }
157
        return parent::php($data);
158
    }
159
}
160