Passed
Push — master ( 4a884d...c9d1fc )
by Jeff
12:14
created

ShippoShipmentRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 63
dl 0
loc 153
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A readyShipment() 0 11 1
A readyParcel() 0 36 3
A __construct() 0 5 1
A getRates() 0 3 1
A setPickupAddress() 0 14 1
A setDeliveryAddress() 0 14 1
1
<?php
2
3
namespace App\Shop\Shipping\Shippo;
4
5
use App\Shop\Addresses\Address;
6
use App\Shop\Customers\Customer;
7
use App\Shop\Products\Product;
8
use App\Shop\Shipping\ShippingInterface;
9
use Illuminate\Support\Collection;
10
use Shippo;
11
use Shippo_Shipment;
12
13
class ShippoShipmentRepository implements ShippingInterface
14
{
15
    /**
16
     * @var Customer
17
     */
18
    protected $customer;
19
20
    /**
21
     * The address where to pick up the item for delivery
22
     *
23
     * @var $warehouseAddress
0 ignored issues
show
Documentation Bug introduced by
The doc comment $warehouseAddress at position 0 could not be parsed: Unknown type name '$warehouseAddress' at position 0 in $warehouseAddress.
Loading history...
24
     */
25
    protected $warehouseAddress;
26
27
    /**
28
     * The address of the customer where the item is to be delivered
29
     *
30
     * @var $deliveryAddress
0 ignored issues
show
Documentation Bug introduced by
The doc comment $deliveryAddress at position 0 could not be parsed: Unknown type name '$deliveryAddress' at position 0 in $deliveryAddress.
Loading history...
31
     */
32
    protected $deliveryAddress;
33
34
    /**
35
     * The item/s
36
     *
37
     * @var $parcel
0 ignored issues
show
Documentation Bug introduced by
The doc comment $parcel at position 0 could not be parsed: Unknown type name '$parcel' at position 0 in $parcel.
Loading history...
38
     */
39
    protected $parcel;
40
41
    /**
42
     * Shipment
43
     *
44
     * @var $shipment
0 ignored issues
show
Documentation Bug introduced by
The doc comment $shipment at position 0 could not be parsed: Unknown type name '$shipment' at position 0 in $shipment.
Loading history...
45
     */
46
    protected $shipment;
47
48
    /**
49
     * ShippoShipment constructor.
50
     *
51
     * @param Customer $customer
52
     */
53
    public function __construct(Customer $customer)
54
    {
55
        Shippo::setApiKey(env('SHIPPO_API_TOKEN'));
56
57
        $this->customer = $customer;
58
    }
59
60
    /**
61
     * Address where the shipment will be picked up
62
     */
63
    public function setPickupAddress()
64
    {
65
        $warehouse = [
66
            'name' => config('app.name'),
67
            'street1' => config('shop.warehouse.address_1'),
68
            'city' => config('shop.warehouse.city'),
69
            'state' => config('shop.warehouse.state'),
70
            'zip' => config('shop.warehouse.zip'),
71
            'country' => config('shop.warehouse.country'),
72
            'phone' => config('shop.phone'),
73
            'email' => config('shop.email')
74
        ];
75
76
        $this->warehouseAddress = $warehouse;
77
    }
78
79
    /**
80
     * @param Address $address
81
     */
82
    public function setDeliveryAddress(Address $address)
83
    {
84
        $delivery =  [
85
            'name' => $address->alias,
86
            'street1' => $address->address_1,
87
            'city' => $address->city,
88
            'state' => $address->state_code,
89
            'zip' => $address->zip,
90
            'country' => $address->country->iso,
91
            'phone' => '',
92
            'email' => $this->customer->email
93
        ];
94
95
        $this->deliveryAddress = $delivery;
96
    }
97
98
    /**
99
     * @return Shippo_Shipment
100
     */
101
    public function readyShipment()
102
    {
103
        $shipment = Shippo_Shipment::create(array(
104
                'address_from'=> $this->warehouseAddress,
105
                'address_to'=> $this->deliveryAddress,
106
                'parcels'=> $this->parcel,
107
                'async'=> false
108
            )
109
        );
110
111
        return $shipment;
112
    }
113
114
    /**
115
     * @param string $id
116
     * @param string $currency
117
     *
118
     * @return \Shippo_Get_Shipping_Rates
0 ignored issues
show
Bug introduced by
The type Shippo_Get_Shipping_Rates 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...
119
     */
120
    public function getRates(string $id, string $currency = 'USD')
121
    {
122
        return Shippo_Shipment::get_shipping_rates(compact('id', 'currency'));
123
    }
124
125
    /**
126
     * @param Collection $collection
127
     *
128
     * @return void
129
     */
130
    public function readyParcel(Collection $collection)
131
    {
132
        $weight = $collection->map(function ($item) {
133
            return [
134
                'weight' => $item->product->weight * $item->qty,
135
                'mass_unit' => $item->product->mass_unit
136
            ];
137
        })->map(function ($item) {
138
            $total = 0;
139
            switch ($item['mass_unit']) {
140
                case Product::MASS_UNIT['OUNCES'] :
141
                    $oz = $item['weight'] / 16;
142
                    $total += $oz;
143
                    break;
144
                case Product::MASS_UNIT['GRAMS'] :
145
                    $oz = $item['weight'] *  0.0022;
146
                    $total += $oz;
147
                    break;
148
                default:
149
                    $total += $item['weight'];
150
            }
151
            return [
152
                'weight' => $total
153
            ];
154
        })->sum('weight');
155
156
        $parcel = array(
157
            'length'=> '5',
158
            'width'=> '5',
159
            'height'=> '5',
160
            'distance_unit'=> 'in',
161
            'weight'=> $weight,
162
            'mass_unit'=> 'lb',
163
        );
164
165
        $this->parcel = $parcel;
166
    }
167
}