Passed
Branch master (249862)
by Adam
07:51
created

ReferController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 17 2
A index() 0 16 2
1
<?php
2
3
namespace Coyote\Http\Controllers\Job;
4
5
use Coyote\Http\Factories\MailFactory;
6
use Coyote\Http\Forms\Job\ReferForm;
7
use Coyote\Job;
8
use Coyote\Mail\OfferReferred;
9
use Coyote\Mail\OfferReferredPerson;
10
use Coyote\Services\UrlBuilder\UrlBuilder;
11
use Coyote\Services\Stream\Activities\Create as Stream_Create;
12
use Coyote\Services\Stream\Objects\Job as Stream_Job;
13
use Coyote\Services\Stream\Objects\Refer as Stream_Refer;
14
15
class ReferController extends BaseController
16
{
17
    use MailFactory;
18
19
    /**
20
     * @param Job $job
21
     * @return \Illuminate\View\View
22
     */
23
    public function index($job)
24
    {
25
        $this->breadcrumb->push([
26
            'Praca' => route('job.home'),
27
            $job->title => UrlBuilder::job($job),
28
            "Poleć znajomego na stanowisko {$job->title}" => null
29
        ]);
30
31
        $form = $this->createForm(ReferForm::class);
32
33
        if ($this->userId) {
34
            $form->get('email')->setValue($this->auth->email);
35
            $form->get('name')->setValue($this->auth->name);
36
        }
37
38
        return $this->view('job.refer')->with(['form' => $form, 'job' => $job]);
39
    }
40
41
    /**
42
     * @param Job $job
43
     * @param ReferForm $form
44
     * @return \Illuminate\Http\RedirectResponse
45
     */
46
    public function save($job, ReferForm $form)
47
    {
48
        $this->transaction(function () use ($job, $form) {
49
            $target = (new Stream_Job)->map($job);
50
51
            $job->refers()->create($form->all() + ['guest_id' => $this->guestId]);
52
            $mailer = $this->getMailFactory();
53
54
            $mailer->to($job->email ?: $job->user->email)->send((new OfferReferred($job))->with($form->all()));
55
            $mailer->to($form->get('friend_email')->getValue())->send((new OfferReferredPerson($job))->with($form->all()));
56
57
            stream(Stream_Create::class, new Stream_Refer(['displayName' => $form->get('friend_name')->getValue()]), $target);
58
        });
59
60
        return redirect()
61
            ->to(UrlBuilder::job($job))
62
            ->with('success', 'Dziękujemy! Zgłoszenie zostało prawidłowo wysłane.');
63
    }
64
}
65