Passed
Push — master ( 4fbb01...435ee1 )
by Adam
12:05
created

BoostJobOffer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 83
rs 10
c 1
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A shouldGeneratePayment() 0 3 3
A handle() 0 43 5
1
<?php
2
3
namespace Coyote\Listeners;
4
5
ini_set('memory_limit', '1G');
6
7
use Carbon\Carbon;
8
use Coyote\Events\PaymentPaid;
9
use Coyote\Notifications\SuccessfulPaymentNotification;
10
use Coyote\Payment;
11
use Coyote\Services\Elasticsearch\Crawler;
12
use Illuminate\Database\Connection;
13
use Illuminate\Contracts\Queue\ShouldQueue;
14
use Coyote\Services\Invoice\Enumerator as InvoiceEnumerator;
15
use Coyote\Services\Invoice\Pdf as InvoicePdf;
16
17
class BoostJobOffer implements ShouldQueue
18
{
19
    /**
20
     * @var int
21
     */
22
    public $delay = 30;
23
24
    /**
25
     * @var InvoiceEnumerator
26
     */
27
    private $enumerator;
28
29
    /**
30
     * @var InvoicePdf
31
     */
32
    private $pdf;
33
34
    /**
35
     * @param InvoiceEnumerator $enumerator
36
     * @param InvoicePdf $pdf
37
     */
38
    public function __construct(InvoiceEnumerator $enumerator, InvoicePdf $pdf)
39
    {
40
        $this->enumerator = $enumerator;
41
        $this->pdf = $pdf;
42
    }
43
44
    /**
45
     * Handle the event.
46
     *
47
     * @param  PaymentPaid  $event
48
     * @return void
49
     */
50
    public function handle(PaymentPaid $event)
51
    {
52
        $payment = $event->payment;
53
54
        app(Connection::class)->transaction(function () use ($payment) {
55
            $pdf = null;
56
57
            // set up invoice only if firm name was provided. it's required!
58
            if ($this->shouldGeneratePayment($payment)) {
59
                // set up invoice number since it's already paid.
60
                $this->enumerator->enumerate($payment->invoice);
61
                // create pdf
62
                $pdf = $this->pdf->create($payment);
63
            }
64
65
            $payment->status_id = Payment::PAID;
66
67
            // establish plan's finish date
68
            $payment->starts_at = Carbon::now();
69
            $payment->ends_at = Carbon::now()->addDays($payment->days);
70
71
            if ($payment->coupon) {
72
                $payment->coupon->delete();
73
            }
74
75
            $payment->save();
76
77
            foreach ($payment->plan->benefits as $benefit) {
78
                if ($benefit !== 'is_social') { // column is_social does not exist in table
79
                    $payment->job->{$benefit} = true;
80
                }
81
            }
82
83
            $payment->job->boost_at = Carbon::now();
84
            $payment->job->deadline_at = max($payment->job->deadline_at, $payment->ends_at);
85
            $payment->job->save();
86
87
            // index job offer
88
            (new Crawler())->index($payment->job);
89
90
            // send email with invoice
91
            $payment->job->user->notify(
92
                new SuccessfulPaymentNotification($payment, $pdf)
93
            );
94
        });
95
    }
96
97
    private function shouldGeneratePayment(Payment $payment): bool
98
    {
99
        return $payment->invoice_id && $payment->invoice->name && $payment->invoice->netPrice();
100
    }
101
}
102