Completed
Push — 1.0 ( 058d33...958294 )
by Morven
02:48
created

MemberExtension::Contact()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A MemberExtension::getContactTitle() 0 5 1
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 $belongs_to = [
0 ignored issues
show
introduced by
The private property $belongs_to is not used, and could be removed.
Loading history...
26
        'Contact' => Contact::class . '.Member'
27
    ];
28
29
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
30
        "ContactTitle" => "Varchar"
31
    ];
32
33
    public function updateCMSFields(FieldList $fields)
34
    {
35
        if ($this->owner->ID) {
36
            $fields->addFieldToTab(
37
                "Root.Main",
38
                ReadonlyField::create("ContactTitle")
39
            );
40
        }
41
    }
42
43
    /**
44
     * The name of the contact assotiated with this account
45
     *
46
     * @return void
47
     */
48
    public function getContactTitle()
49
    {
50
        $contact = $this->owner->Contact();
51
52
        return $contact->Title;
53
    }
54
55
    /**
56
     * Get a discount from the groups this member is in
57
     *
58
     * @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...
59
     */
60
    public function getDiscount()
61
    {
62
        $discounts = ArrayList::create();
63
64
        foreach ($this->owner->Groups() as $group) {
65
            foreach ($group->Discounts() as $discount) {
66
                $discounts->add($discount);
67
            }
68
        }
69
70
        $discounts->sort("Amount", "DESC");
71
72
        return $discounts->first();
73
    }
74
75
    /**
76
     * Get all invoices from a contact that are designated
77
     * "outstanding"
78
     *
79
     * @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...
80
     */
81
    public function OutstandingInvoices()
82
    {
83
        return $this
84
            ->owner
85
            ->Contact()
86
            ->OutstandingInvoices();
87
    }
88
89
    /**
90
     * Get all invoices from a contact that are designated
91
     * "historic"
92
     *
93
     * @return DataList
94
     */
95
    public function HistoricInvoices()
96
    {
97
        return $this
98
            ->owner
99
            ->Contact()
100
            ->HistoricInvoices();
101
    }
102
103
    /**
104
     * If no contact exists for this account, then create one
105
     *
106
     * @return void
107
     */
108
    public function onAfterWrite()
109
    {
110
        parent::onAfterWrite();
111
112
        if (!$this->Contact()->exists()) {
0 ignored issues
show
Bug introduced by
The method Contact() does not exist on SilverCommerce\OrdersAdm...ensions\MemberExtension. Did you maybe mean getContactTitle()? ( Ignorable by Annotation )

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

112
        if (!$this->/** @scrutinizer ignore-call */ Contact()->exists()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
            $contact = Contact::create([
114
                "FirstName" => $this->owner->FirstName,
115
                "Surname" => $this->owner->Surname,
116
                "Email" => $this->owner->Email
117
            ]);
118
            $contact->MemberID = $this->owner->ID;
119
            $contact->write();
120
        }
121
    }
122
}
123