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

FlatRate::getPossiblePostage()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 8
nop 1
dl 0
loc 32
rs 8.439
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 SilverCommerce\Postage\Helpers\Parcel;
9
use SilverCommerce\Postage\Model\PostageType;
10
use SilverCommerce\Postage\Helpers\PostageOption;
11
12
/**
13
 * Represents a flat shipping cost, based on the selected regions.
14
 * 
15
 * NOTE If you dont select any regions, this rate will be applied to
16
 * ALL regions
17
 */
18
class FlatRate extends PostageType
19
{
20
    private static $table_name = 'PostageType_FlatRate';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
21
22
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
23
        "Price" => "Currency"
24
    ];
25
26
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
27
        "Locations" => Zone::class
28
    ];
29
30
    /**
31
     * If the current parcel is located in an area that we
32
     * allow flat rate
33
     * 
34
     * @param Parcel
35
     * @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...
36
     */
37
    public function getPossiblePostage(Parcel $parcel)
38
    {
39
        $return = ArrayList::create();
40
        $locations = $this->Locations();
0 ignored issues
show
Bug introduced by
The method Locations() does not exist on SilverCommerce\Postage\Model\FlatRate. 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

40
        /** @scrutinizer ignore-call */ 
41
        $locations = $this->Locations();
Loading history...
41
        $country = $parcel->getCountry();
42
        $region = $parcel->getRegion();
43
        $tax = null;
44
45
        if ($this->Tax()->exists()) {
0 ignored issues
show
Bug introduced by
The method Tax() does not exist on SilverCommerce\Postage\Model\FlatRate. 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

45
        if ($this->/** @scrutinizer ignore-call */ Tax()->exists()) {
Loading history...
46
            $tax = $this->Tax()->ValidTax();
47
        }
48
        
49
        $postage = PostageOption::create(
50
            $this->Name,
0 ignored issues
show
Bug Best Practice introduced by
The property Name does not exist on SilverCommerce\Postage\Model\FlatRate. Since you implemented __get, consider adding a @property annotation.
Loading history...
51
            $this->Price,
0 ignored issues
show
Bug Best Practice introduced by
The property Price does not exist on SilverCommerce\Postage\Model\FlatRate. Since you implemented __get, consider adding a @property annotation.
Loading history...
52
            $tax
53
        ); 
54
55
        if (!$locations->exists()) {
56
            $return->add($postage);
57
        } elseif (isset($country) && isset($region)) {
58
            $locations = $locations->filter([
59
                "Regions.CountryCode" => $country,
60
                "Regions.Code" => $region
61
            ]);
62
63
            if ($locations->exists()) {
64
                $return->add($postage);
65
            }
66
        }
67
68
        return $return;
69
    }
70
}