Passed
Branch 1.0 (badbbc)
by Morven
01:40
created

PriceBased::getPossiblePostage()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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

71
        /** @scrutinizer ignore-call */ 
72
        $locations = $this->Locations();
Loading history...
72
        $country = $parcel->getCountry();
73
        $region = $parcel->getRegion();
74
        $value = (float)$parcel->getItems();
75
        $check = false;
76
        $tax = null;
77
78
        if ($this->Tax()->exists()) {
0 ignored issues
show
Bug introduced by
The method Tax() does not exist on SilverCommerce\Postage\Model\PriceBased. 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

78
        if ($this->/** @scrutinizer ignore-call */ Tax()->exists()) {
Loading history...
79
            $tax = $this->Tax()->ValidTax();
80
        }
81
        
82
        // Should this type filter based on location
83
        if (!$locations->exists()) {
84
            $check = true;
85
        } elseif (isset($country) && isset($region)) {
86
            $locations = $locations->filter([
87
                "Regions.CountryCode" => $country,
88
                "Regions.Code" => $region
89
            ]);
90
91
            if ($locations->exists()) {
92
                $check = true;
93
            }
94
        }
95
96
        if ($check) {
97
            $rates = $this->Rates()->filter([
0 ignored issues
show
Bug introduced by
The method Rates() does not exist on SilverCommerce\Postage\Model\PriceBased. 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

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