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

ApplicationController::save()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 1
nop 2
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Http\Controllers\Job;
4
5
use Coyote\Http\Controllers\Controller;
6
use Coyote\Http\Factories\MailFactory;
7
use Coyote\Http\Forms\Job\ApplicationForm;
8
use Coyote\Job;
9
use Coyote\Notifications\ApplicationConfirmationNotification;
10
use Coyote\Notifications\ApplicationSentNotification;
11
use Coyote\Services\UrlBuilder\UrlBuilder;
12
use Illuminate\Http\Request;
13
use Coyote\Services\Stream\Activities\Create as Stream_Create;
14
use Coyote\Services\Stream\Objects\Job as Stream_Job;
15
use Coyote\Services\Stream\Objects\Application as Stream_Application;
16
17
class ApplicationController extends Controller
18
{
19
    use MailFactory;
20
21
    public function __construct()
22
    {
23
        parent::__construct();
24
25
        $this->middleware(
26
            function (Request $request, $next) {
27
                /** @var \Coyote\Job $job */
28
                $job = $request->route('job');
29
                abort_if($job->applications()->forGuest($this->guestId)->exists(), 404);
30
31
                return $next($request);
32
            },
33
            ['except' => 'upload']
34
        );
35
    }
36
37
    /**
38
     * @param Job $job
39
     * @return \Illuminate\View\View
40
     */
41
    public function submit($job)
42
    {
43
        abort_if(!$job->enable_apply, 404);
44
45
        $this->breadcrumb->push([
46
            'Praca'                             => route('job.home'),
47
            $job->title                         => UrlBuilder::job($job),
48
            'Aplikuj na to stanowisko pracy'    => null
49
        ]);
50
51
        /**
52
         * @var ApplicationForm $form
53
         */
54
        $form = $this->createForm(ApplicationForm::class);
55
56
        if ($this->userId) {
57
            $form->get('email')->setValue($this->auth->email);
58
            $form->get('github')->setValue($this->auth->github);
59
        }
60
61
        // set default message
62
        $form->get('text')->setValue(view('job.partials.application', compact('job')));
63
64
        if ($this->getSetting('job.application')) {
65
            $form->setData(json_decode($this->getSetting('job.application')));
66
        }
67
68
        return $this->view('job.application', compact('job', 'form'))->with(
69
            'subscribed',
70
            $this->userId ? $job->subscribers()->forUser($this->userId)->exists() : false
71
        );
72
    }
73
74
    /**
75
     * @param Job $job
76
     * @param ApplicationForm $form
77
     * @return \Illuminate\Http\RedirectResponse
78
     */
79
    public function save($job, ApplicationForm $form)
80
    {
81
        $data = $form->all() + ['guest_id' => $this->guestId];
82
83
        $this->transaction(function () use ($job, $form, $data) {
84
            $target = (new Stream_Job)->map($job);
85
86
            /** @var \Coyote\Job\Application $application */
87
            $application = $job->applications()->create($data);
88
89
            $this->setSetting('job.application', $form->get('remember')->isChecked() ? $form->toJson() : '');
90
91
            $job->notify(new ApplicationSentNotification($application));
92
            $application->notify(new ApplicationConfirmationNotification());
93
94
            stream(Stream_Create::class, new Stream_Application(['displayName' => $data['name']]), $target);
95
        });
96
97
        return redirect()
98
            ->route('job.offer', [$job->id, $job->slug])
99
            ->with('success', 'Zgłoszenie zostało prawidłowo wysłane.');
100
    }
101
102
    /**
103
     * Upload cv/resume
104
     *
105
     * @param Request $request
106
     * @return \Illuminate\Http\JsonResponse
107
     */
108
    public function upload(Request $request)
109
    {
110
        $this->validate($request, [
111
            // only 5 MB file size limit. otherwise postfix may not handle it properly.
112
            'cv'             => 'max:' . (5 * 1024) . '|mimes:pdf,doc,docx,rtf'
113
        ]);
114
115
        $filename = uniqid() . '_' . $request->file('cv')->getClientOriginalName();
116
        $request->file('cv')->storeAs('cv', $filename, 'local');
117
118
        return response()->json([
119
            'filename' => $filename,
120
            'name' => $request->file('cv')->getClientOriginalName()
121
        ]);
122
    }
123
}
124