Passed
Push — master ( d6ac80...a7dfb7 )
by Adam
11:29
created

ApplicationController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 13
rs 10
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\Requests\ApplicationRequest;
8
use Coyote\Job;
9
use Coyote\Notifications\Job\ApplicationConfirmationNotification;
10
use Coyote\Notifications\Job\ApplicationSentNotification;
11
use Coyote\Services\UrlBuilder;
12
use Illuminate\Filesystem\FilesystemManager;
13
use Illuminate\Http\Request;
14
use Coyote\Services\Stream\Activities\Create as Stream_Create;
15
use Coyote\Services\Stream\Objects\Job as Stream_Job;
16
use Coyote\Services\Stream\Objects\Application as Stream_Application;
17
use Illuminate\Support\Str;
18
19
class ApplicationController extends Controller
20
{
21
    use MailFactory;
22
23
    /**
24
     * @param Job $job
25
     * @return \Illuminate\View\View
26
     */
27
    public function submit($job)
28
    {
29
        abort_if(!$job->enable_apply, 404);
30
31
        $this->breadcrumb->push([
32
            'Praca'                             => route('job.home'),
33
            $job->title                         => UrlBuilder::job($job),
34
            'Aplikuj na to stanowisko pracy'    => null
35
        ]);
36
37
        $application = new Job\Application();
38
39
        if ($this->userId) {
40
            $application->email = $this->auth->email;
41
            $application->github = $this->auth->github;
42
        }
43
44
        // set default message
45
        $application->text = view('job.partials.application', compact('job'))->render();
0 ignored issues
show
Documentation Bug introduced by
It seems like view('job.partials.appli...mpact('job'))->render() can also be of type array. However, the property $text is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
46
47
        if ($this->getSetting('job.application')) {
48
            $application->forceFill((array) json_decode($this->getSetting('job.application')));
0 ignored issues
show
Bug introduced by
It seems like $this->getSetting('job.application') can also be of type null; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

48
            $application->forceFill((array) json_decode(/** @scrutinizer ignore-type */ $this->getSetting('job.application')));
Loading history...
49
        }
50
51
        return $this->view('job.application', compact('job', 'application'))->with([
52
            'subscribed' => $this->userId ? $job->subscribers()->forUser($this->userId)->exists() : false
53
        ]);
54
    }
55
56
    /**
57
     * @param Job $job
58
     * @param ApplicationRequest $request
59
     * @return string
60
     */
61
    public function save(Job $job, ApplicationRequest $request)
62
    {
63
        $application = $this->transaction(function () use ($job, $request) {
64
            $target = (new Stream_Job)->map($job);
65
66
            /** @var \Coyote\Job\Application $application */
67
            $application = $job->applications()->create($request->all() + ['guest_id' => $this->guestId]);
68
            $this->setSetting('job.application', $request->get('remember') ? json_encode($request->all()) : '');
69
70
            stream(Stream_Create::class, new Stream_Application(['displayName' => $request->input('name')]), $target);
71
72
            return $application;
73
        });
74
75
        $job->notify(new ApplicationSentNotification($application));
0 ignored issues
show
Bug introduced by
It seems like $application can also be of type null; however, parameter $application of Coyote\Notifications\Job...fication::__construct() does only seem to accept Coyote\Job\Application, maybe add an additional type check? ( Ignorable by Annotation )

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

75
        $job->notify(new ApplicationSentNotification(/** @scrutinizer ignore-type */ $application));
Loading history...
76
        $application->notify(new ApplicationConfirmationNotification());
77
78
        session()->flash('success', 'Zgłoszenie zostało prawidłowo wysłane.');
79
80
        return UrlBuilder::job($job);
81
    }
82
83
    /**
84
     * Upload cv/resume
85
     *
86
     * @param Request $request
87
     * @return \Illuminate\Http\JsonResponse
88
     */
89
    public function upload(Request $request)
90
    {
91
        $this->validate($request, [
92
            // only 5 MB file size limit. otherwise postfix may not handle it properly.
93
            'cv'             => 'max:' . (5 * 1024) . '|mimes:pdf,doc,docx,rtf'
94
        ]);
95
96
        $filename = uniqid() . '_' . Str::ascii($request->file('cv')->getClientOriginalName());
97
        $request->file('cv')->storeAs('cv', $filename, 'local');
98
99
        return response()->json([
100
            'filename' => $filename,
101
            'name' => $request->file('cv')->getClientOriginalName()
102
        ]);
103
    }
104
105
    /**
106
     * @param FilesystemManager $filesystem
107
     * @param Job $job
108
     * @param $id
109
     * @return mixed
110
     */
111
    public function downloadApplication(FilesystemManager $filesystem, Job $job, $id)
112
    {
113
        abort_if($job->user_id !== $this->userId, 403);
114
115
        /** @var \Coyote\Job\Application $application */
116
        $application = $job->applications()->find($id);
117
118
        return $filesystem->disk('local')->download('cv/' . $application->cv, $application->realFilename());
119
    }
120
}
121