WeightBased::getPossiblePostage()   B
last analyzed

Complexity

Conditions 8
Paths 32

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 8
eloc 33
c 4
b 0
f 0
nc 32
nop 1
dl 0
loc 57
rs 8.1475

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverCommerce\Postage\Model;
4
5
use SilverStripe\ORM\ArrayList;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\SiteConfig\SiteConfig;
8
use SilverCommerce\Postage\Helpers\Parcel;
9
use SilverCommerce\TaxAdmin\Model\TaxCategory;
10
use SilverCommerce\TaxAdmin\Helpers\MathsHelper;
11
use SilverCommerce\Postage\Helpers\PostageOption;
12
use SilverStripe\Forms\GridField\GridFieldDetailForm;
13
use SilverStripe\Forms\GridField\GridFieldEditButton;
14
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
15
use SilverStripe\Forms\GridField\GridFieldDataColumns;
16
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
17
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
18
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
19
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
20
21
/**
22
 * Postage objects list available postage costs and destination locations
23
 *
24
 */
25
class WeightBased extends PostageType
26
{
27
    private static $table_name = 'PostageType_WeightBased';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
28
29
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
30
        "Rates" => SinglePostageRate::class
31
    ];
32
33
    public function getCMSFields()
34
    {
35
        $this->beforeUpdateCMSFields(function ($fields) {
36
            $rates_field = $fields->dataFieldByName("Rates");
37
38
            if (isset($rates_field)) {
39
                $config = $rates_field->getConfig();
40
                $config
41
                    ->removeComponentsByType(GridFieldDetailForm::class)
42
                    ->removeComponentsByType(GridFieldEditButton::class)
43
                    ->removeComponentsByType(GridFieldDataColumns::class)
44
                    ->removeComponentsByType(GridFieldDeleteAction::class)
45
                    ->removeComponentsByType(GridFieldAddNewButton::class)
46
                    ->removeComponentsByType(GridFieldAddExistingAutocompleter::class)
47
                    ->addComponent(new GridFieldEditableColumns())
48
                    ->addComponent(new GridFieldAddNewInlineButton())
49
                    ->addComponent(new GridFieldDeleteAction());
50
            }
51
        });
52
53
        return parent::getCMSFields();
54
    }
55
56
    /**
57
     * If the current parcel is located in an area that we
58
     * allow and has an acceptable weight
59
     *
60
     * @param Parcel
61
     * @return SSList
0 ignored issues
show
Bug introduced by
The type SilverCommerce\Postage\Model\SSList 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...
62
     */
63
    public function getPossiblePostage(Parcel $parcel)
64
    {
65
        $return = ArrayList::create();
66
        $locations = $this->Locations();
0 ignored issues
show
Bug introduced by
The method Locations() does not exist on SilverCommerce\Postage\Model\WeightBased. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

66
        /** @scrutinizer ignore-call */ 
67
        $locations = $this->Locations();
Loading history...
67
        $exclude = $this->Exclusions();
0 ignored issues
show
Bug introduced by
The method Exclusions() does not exist on SilverCommerce\Postage\Model\WeightBased. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

67
        /** @scrutinizer ignore-call */ 
68
        $exclude = $this->Exclusions();
Loading history...
68
        $country = $parcel->getCountry();
69
        $region = $parcel->getRegion();
70
        $value = (float)$parcel->getWeight();
71
        $check = false;
72
        $tax = null;
73
74
        if ($this->Tax()->exists()) {
0 ignored issues
show
Bug introduced by
The method Tax() does not exist on SilverCommerce\Postage\Model\WeightBased. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

74
        if ($this->/** @scrutinizer ignore-call */ Tax()->exists()) {
Loading history...
75
            $tax = $this->Tax()->ValidTax();
76
        }
77
78
        if ($exclude->exists()) {
79
            $exclude = $exclude->filter(
80
                [
81
                    "Regions.CountryCode" => $country,
82
                    "Regions.Code" => $region
83
                ]
84
            );
85
        }
86
87
        if (!$exclude->exists()) {
88
            if ($locations->exists()) {
89
                $locations = $locations->filter(
90
                    [
91
                        "Regions.CountryCode" => $country,
92
                        "Regions.Code" => $region
93
                    ]
94
                );
95
96
                if ($locations->exists()) {
97
                    $check = true;
98
                }
99
            } else {
100
                $check = true;
101
            }
102
        }
103
104
        if ($check) {
105
            $rates = $this->Rates()->filter([
0 ignored issues
show
Bug introduced by
The method Rates() does not exist on SilverCommerce\Postage\Model\WeightBased. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

105
            $rates = $this->/** @scrutinizer ignore-call */ Rates()->filter([
Loading history...
106
                "Min:LessThanOrEqual" => $value,
107
                "Max:GreaterThanOrEqual" => $value
108
            ]);
109
110
            foreach ($rates as $rate) {
111
                $return->add(PostageOption::create(
112
                    $this->Name,
0 ignored issues
show
Bug Best Practice introduced by
The property Name does not exist on SilverCommerce\Postage\Model\WeightBased. Since you implemented __get, consider adding a @property annotation.
Loading history...
113
                    $rate->Price,
114
                    $tax
115
                ));
116
            }
117
        }
118
119
        return $return;
120
    }
121
}
122