Passed
Pull Request — master (#71)
by Adeniyi
05:43
created

StripeRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 35 3
A __construct() 0 3 1
1
<?php
2
3
namespace App\Shop\PaymentMethods\Stripe;
4
5
use App\Shop\Checkout\CheckoutRepository;
6
use App\Shop\Couriers\Courier;
7
use App\Shop\Couriers\Repositories\CourierRepository;
8
use App\Shop\Customers\Customer;
9
use App\Shop\Customers\Repositories\CustomerRepository;
10
use App\Shop\PaymentMethods\Stripe\Exceptions\StripeChargingErrorException;
11
use Gloudemans\Shoppingcart\Facades\Cart;
12
use Ramsey\Uuid\Uuid;
13
use Stripe\Charge;
14
15
class StripeRepository
16
{
17
    /**
18
     * @var Customer
19
     */
20
    private $customer;
21
22
    /**
23
     * StripeRepository constructor.
24
     * @param Customer $customer
25
     */
26
    public function __construct(Customer $customer)
27
    {
28
        $this->customer = $customer;
29
    }
30
31
    /**
32
     * @param array $data Cart data
33
     * @param $total float Total items in the cart
34
     * @param $tax float The tax applied to the cart
35
     * @return Charge Stripe charge object
36
     * @throws StripeChargingErrorException
37
     */
38
    public function execute(array $data, $total, $tax) : Charge
39
    {
40
        try {
41
            $courierRepo = new CourierRepository(new Courier);
42
            $courierId = $data['courier'];
43
            $courier = $courierRepo->findCourierById($courierId);
44
45
            $totalComputed = $total + $courier->cost;
46
47
            $customerRepo = new CustomerRepository($this->customer);
48
            $options['source'] = $data['stripeToken'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.
Loading history...
49
            $options['currency'] = config('cart.currency');
50
51
            if ($charge = $customerRepo->charge($totalComputed, $options)) {
0 ignored issues
show
Bug introduced by
$totalComputed of type double is incompatible with the type integer expected by parameter $amount of App\Shop\Customers\Repos...merRepository::charge(). ( Ignorable by Annotation )

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

51
            if ($charge = $customerRepo->charge(/** @scrutinizer ignore-type */ $totalComputed, $options)) {
Loading history...
52
                $checkoutRepo = new CheckoutRepository;
53
                $checkoutRepo->buildCheckoutItems([
54
                    'reference' => Uuid::uuid4()->toString(),
55
                    'courier_id' => $courierId,
56
                    'customer_id' => $this->customer->id,
57
                    'address_id' => $data['billing_address'],
58
                    'order_status_id' => 1,
59
                    'payment' => strtolower(config('stripe.name')),
60
                    'discounts' => 0,
61
                    'total_products' => $total,
62
                    'total' => $totalComputed,
63
                    'total_paid' => $totalComputed,
64
                    'tax' => $tax
65
                ]);
66
67
                Cart::destroy();
68
            }
69
70
            return $charge;
71
        } catch (\Exception $e) {
72
            throw new StripeChargingErrorException($e);
73
        }
74
    }
75
}
76