Passed
Push — master ( f2dd02...330c2d )
by Adam
12:12
created

CreatedNotification::redirectionUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Notifications\Job;
4
5
use Coyote\Job;
6
use Coyote\Services\Invoice\Calculator;
7
use Coyote\Services\Invoice\CalculatorFactory;
8
use Coyote\Services\Notification\Notification;
9
use Coyote\Services\UrlBuilder;
10
use Coyote\User;
11
use Illuminate\Bus\Queueable;
12
use Illuminate\Contracts\Queue\ShouldQueue;
13
use Illuminate\Notifications\Messages\MailMessage;
14
use Coyote\Services\Notification\DatabaseChannel;
15
16
class CreatedNotification extends Notification implements ShouldQueue
17
{
18
    use Queueable;
19
20
    const ID = \Coyote\Notification::JOB_CREATE;
21
22
    /**
23
     * @var Job
24
     */
25
    private $job;
26
27
    /**
28
     * @var Calculator
29
     */
30
    private $calculator;
31
32
    /**
33
     * @param Job $job
34
     */
35
    public function __construct(Job $job)
36
    {
37
        $this->job = $job;
38
    }
39
40
    /**
41
     * @param User $user
42
     * @return array
43
     */
44
    public function via(User $user)
45
    {
46
        $payment = $this->job->getUnpaidPayment();
47
48
        if (!$payment) {
0 ignored issues
show
introduced by
$payment is of type Coyote\Payment, thus it always evaluated to true.
Loading history...
49
            return [];
50
        }
51
52
        // calculate price based on payment details
53
        $this->calculator = CalculatorFactory::payment($payment);
54
55
        if (!$this->calculator->grossPrice()) {
56
            return [];
57
        }
58
59
        return ['mail', DatabaseChannel::class];
60
    }
61
62
    /**
63
     * @param \Coyote\User $user
64
     * @return array
65
     */
66
    public function toDatabase($user)
67
    {
68
        return [
69
            'object_id'     => $this->objectId(),
70
            'user_id'       => $user->id,
71
            'type_id'       => static::ID,
72
            'subject'       => $this->job->title,
73
            'excerpt'       => 'Ogłoszenie zostało dodane i oczekuje na płatność',
74
            'url'           => UrlBuilder::job($this->job),
75
            'id'            => $this->id
76
        ];
77
    }
78
79
    /**
80
     * Generowanie unikalnego ciagu znakow dla wpisu na mikro
81
     *
82
     * @return string
83
     */
84
    public function objectId()
85
    {
86
        return substr(md5(static::ID . $this->job->id), 16);
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function sender()
93
    {
94
        return [
95
            'user_id'       => $this->job->user_id,
96
            'name'          => $this->job->user->name
97
        ];
98
    }
99
100
    /**
101
     * Get the mail representation of the notification.
102
     *
103
     * @return \Illuminate\Notifications\Messages\MailMessage
104
     */
105
    public function toMail()
106
    {
107
        return (new MailMessage)
108
            ->subject(sprintf('Ogłoszenie "%s" zostało dodane i oczekuje na płatność', $this->job->title))
109
            ->line(sprintf('Dziękujemy za dodanie ogłoszenia w serwisie <strong>%s</strong>.', config('app.name')))
110
            ->line(
111
                sprintf(
112
                    'Ogłoszenie %s zostało dodane i czeka dokonanie opłaty w kwocie %s zł.',
113
                    link_to(UrlBuilder::job($this->job), $this->job->title),
114
                    $this->calculator->netPrice()
115
                )
116
            )
117
            ->action('Opłać ogłoszenie', route('job.payment', [$this->job->getUnpaidPayment()]))
118
            ->line('Dziękujemy za skorzystanie z naszych usług!');
119
    }
120
121
    protected function redirectionUrl(): string
122
    {
123
        return route('user.notifications.redirect', ['path' => UrlBuilder::job($this->job)]);
124
    }
125
}
126