Passed
Push — 1.0 ( 07b100...83f8df )
by Morven
05:31
created

MemberExtension   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 6 2
A HistoricInvoices() 0 6 1
A getContactTitle() 0 5 1
A OutstandingInvoices() 0 6 1
A getDiscount() 0 13 3
A onAfterWrite() 0 12 2
A Contact() 0 11 2
1
<?php
2
3
namespace SilverCommerce\OrdersAdmin\Extensions;
4
5
use SilverStripe\ORM\DataExtension;
6
use SilverStripe\Forms\TextField;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\ORM\ArrayList;
9
use SilverStripe\Forms\ReadonlyField;
10
use SilverStripe\Core\Config\Config;
11
use SilverCommerce\ContactAdmin\Model\Contact;
12
use SilverCommerce\OrdersAdmin\Model\Invoice;
13
use SilverCommerce\OrdersAdmin\Model\Estimate;
14
use SilverCommerce\OrdersAdmin\Model\MemberAddress;
0 ignored issues
show
Bug introduced by
The type SilverCommerce\OrdersAdmin\Model\MemberAddress was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
/**
17
 * Add additional settings to a memeber object
18
 * 
19
 * @package orders-admin
20
 * @subpackage extensions
21
 */
22
class MemberExtension extends DataExtension
23
{
24
25
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
26
        "ContactTitle" => "Varchar"
27
    ];
28
29
    public function updateCMSFields(FieldList $fields)
30
    {
31
        if ($this->owner->ID) {
32
            $fields->addFieldToTab(
33
                "Root.Main",
34
                ReadonlyField::create("ContactTitle")
0 ignored issues
show
Bug introduced by
'ContactTitle' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
                ReadonlyField::create(/** @scrutinizer ignore-type */ "ContactTitle")
Loading history...
35
            );
36
        }
37
    }
38
39
    /**
40
     * Find a contact associated with this account
41
     *
42
     * @return Contact
43
     */
44
    public function Contact()
45
    {
46
        $contact = Contact::get()
47
            ->find("MemberID", $this->owner->ID);
48
        
49
        if (!$contact) {
0 ignored issues
show
introduced by
The condition ! $contact can never be false.
Loading history...
50
            $contact = Contact::create();
51
            $contact->ID = -1;
52
        }
53
54
        return $contact;
55
    }
56
57
    /**
58
     * The name of the contact assotiated with this account
59
     *
60
     * @return void
61
     */
62
    public function getContactTitle()
63
    {
64
        $contact = $this->owner->Contact();
65
66
        return $contact->Title;
67
    }
68
69
    /**
70
     * Get a discount from the groups this member is in
71
     *
72
     * @return Discount
0 ignored issues
show
Bug introduced by
The type SilverCommerce\OrdersAdmin\Extensions\Discount was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
73
     */
74
    public function getDiscount()
75
    {
76
        $discounts = ArrayList::create();
77
78
        foreach ($this->owner->Groups() as $group) {
79
            foreach ($group->Discounts() as $discount) {
80
                $discounts->add($discount);
81
            }
82
        }
83
84
        $discounts->sort("Amount", "DESC");
85
86
        return $discounts->first();
87
    }
88
89
    /**
90
     * Get all invoices from a contact that are designated
91
     * "outstanding" 
92
     *
93
     * @return DataList
0 ignored issues
show
Bug introduced by
The type SilverCommerce\OrdersAdmin\Extensions\DataList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
94
     */
95
    public function OutstandingInvoices()
96
    {
97
        return $this
98
            ->owner
99
            ->Contact()
100
            ->OutstandingInvoices();
101
    }
102
103
    /**
104
     * Get all invoices from a contact that are designated
105
     * "historic"
106
     *
107
     * @return DataList
108
     */
109
    public function HistoricInvoices()
110
    {
111
        return $this
112
            ->owner
113
            ->Contact()
114
            ->HistoricInvoices();
115
    }
116
117
    /**
118
     * If no contact exists for this account, then create one
119
     *
120
     * @return void
121
     */
122
    public function onAfterWrite()
123
    {
124
        parent::onAfterWrite();
125
126
        if (!$this->Contact()->exists()) {
127
            $contact = Contact::create([
128
                "FirstName" => $this->owner->FirstName,
129
                "Surname" => $this->owner->Surname,
130
                "Email" => $this->owner->Email
131
            ]);
132
            $contact->MemberID = $this->owner->ID;
133
            $contact->write();
134
        }
135
    }
136
}
137