EventApplicationCreated::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright https://yawik.org/COPYRIGHT.php
8
 */
9
10
/** */
11
namespace Applications\Listener;
12
13
use Applications\Entity\Settings;
14
use Applications\Entity\StatusInterface;
15
use Applications\Entity\Application;
16
use Applications\Mail\Confirmation;
17
use Core\Mail\MailService;
18
use Applications\Listener\Events\ApplicationEvent;
19
use Applications\Options\ModuleOptions;
20
use Auth\Entity\UserInterface;
21
use Organizations\Entity\EmployeeInterface;
22
use Organizations\Entity\EmployeePermissionsInterface;
23
24
/**
25
 * This Listener sends mails to various users if a new application is created.
26
 *
27
 * @author Bleek Carsten <[email protected]>
28
 * @author Mathias Gelhausen <[email protected]>
29
 * @todo write test
30
 */
31
class EventApplicationCreated
32
{
33
    /**
34
     * @var Application $application
35
     */
36
    protected $application;
37
38
    /**
39
     * The mail service
40
     *
41
     * @var \Core\Mail\MailService
42
     */
43
    protected $mailService;
44
45
    /**
46
     *
47
     * @param MailService   $mailService
48
     */
49
    public function __construct(MailService $mailService)
50
    {
51
        $this->mailService = $mailService;
52
    }
53
54
    public function __invoke(ApplicationEvent $event)
55
    {
56
        $this->application = $event->getApplicationEntity();
57
        $this->sendRecruiterMails();
58
        $this->sendCarbonCopyToCandidate();
59
    }
60
61
    protected function sendRecruiterMails()
62
    {
63
        /* @var Settings $adminSettings */
64
        $job = $this->application->getJob();
65
        $org = $job->getOrganization()->getParent(/*returnSelf*/ true);
0 ignored issues
show
Unused Code introduced by
The call to Organizations\Entity\Org...nInterface::getParent() has too many arguments starting with true. ( Ignorable by Annotation )

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

65
        $org = $job->getOrganization()->/** @scrutinizer ignore-call */ getParent(/*returnSelf*/ true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
66
        $workflow = $org->getWorkflowSettings();
67
        $admin = $org->getUser();
68
        $adminSettings = $admin->getSettings('Applications');
69
        $mailBcc = $adminSettings->getMailBCC();
0 ignored issues
show
Unused Code introduced by
The assignment to $mailBcc is dead and can be removed.
Loading history...
70
71
        if ($workflow->getAcceptApplicationByDepartmentManager()) {
72
            /* Send mail to department manager, if there is at least one. */
73
            $assignedManagers = $job->getMetaData('organizations:managers', []);
0 ignored issues
show
Unused Code introduced by
The call to Core\Entity\MetaDataProv...nterface::getMetaData() has too many arguments starting with array(). ( Ignorable by Annotation )

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

73
            /** @scrutinizer ignore-call */ 
74
            $assignedManagers = $job->getMetaData('organizations:managers', []);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
74
            if (count($assignedManagers)) {
0 ignored issues
show
Bug introduced by
It seems like $assignedManagers can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, 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

74
            if (count(/** @scrutinizer ignore-type */ $assignedManagers)) {
Loading history...
75
                $managers = [];
76
                foreach ($assignedManagers as $manager) {
77
                    $manager = $org->getEmployee($manager['id']);
78
                    if ($manager) {
79
                        $managers[] = $manager;
80
                    }
81
                }
82
            } else {
83
                $managers = $org->getEmployeesByRole(EmployeeInterface::ROLE_DEPARTMENT_MANAGER);
84
            }
85
86
            if (count($managers)) {
87
                foreach ($managers as $employee) {
88
                    /* @var EmployeeInterface $employee */
89
                    $this->mailService->send(
90
                        'Applications/NewApplication',
91
                        [
92
                            'application' => $this->application,
93
                            'user' => $employee->getUser(),
94
                            'bcc' => $adminSettings->getMailBCC() ? [ $admin ] : null,
95
                        ]
96
                    );
97
                }
98
                return;
99
            }
100
        }
101
102
        if ($workflow->getAcceptApplicationByRecruiters()) {
103
            $recruiters = $org->getEmployeesByRole(EmployeeInterface::ROLE_RECRUITER);
104
            /** @var \Organizations\Entity\Employee $recruiter */
105
            foreach ($recruiters as $recruiter) {
106
                if (!$recruiter->getPermissions()->isAllowed(EmployeePermissionsInterface::APPLICATIONS_CHANGE)
107
                    || $recruiter->getUser()->getRole() != UserInterface::ROLE_RECRUITER
108
                ) {
109
                    continue;
110
                }
111
112
                $this->mailService->send(
113
                    'Applications/NewApplication',
114
                    [
115
                        'application' => $this->application,
116
                        'user' => $recruiter->getUser(),
117
                        'bcc' => $adminSettings->getMailBCC() ? [ $admin ] : null,
118
                    ]
119
                );
120
            }
121
        }
122
123
        $recruiter = $job->getUser();
124
        /* @var \Applications\Entity\Settings $settings */
125
        $settings = $recruiter->getSettings('Applications');
126
        if ($settings->getMailAccess()) {
127
            $this->mailService->send(
128
                'Applications/NewApplication',
129
                [
130
                    'job'   => $this->application->getJob(),
131
                    'user'  => $recruiter,
132
                    'bcc' => $adminSettings->getMailBCC() ? [ $admin ] : null,
133
                ]
134
            );
135
        }
136
137
        if ($settings->getAutoConfirmMail()) {
138
            $ackBody = $settings->getMailConfirmationText();
139
            if (empty($ackBody)) {
140
                $ackBody = $settings->getMailConfirmationText();
141
            }
142
            if (!empty($ackBody)) {
143
                /* confirmation mail to the applicant */
144
                $ackMail = $this->mailService->get(
145
                    Confirmation::class,
146
                    [
147
                        'application' => $this->application,
148
                        'body'        => $ackBody,
149
                        'bcc'         => $adminSettings->getMailBCC() ? [ $admin ] : null,
150
                    ]
151
                );
152
153
                // Must be called after initializers in creation
154
                $ackMail->setSubject(/* @translate */ 'Application confirmation');
155
156
                $ackMail->setFrom($recruiter->getInfo()->getEmail(), $recruiter->getInfo()->getDisplayName(false));
157
                $this->mailService->send($ackMail);
158
                $this->application->changeStatus(
159
                    StatusInterface::CONFIRMED,
160
                    sprintf('Mail was sent to %s', $this->application->getContact()->getEmail())
161
                );
162
            }
163
        }
164
    }
165
166
    /**
167
     * Send Carbon Copy to the User
168
     */
169
    protected function sendCarbonCopyToCandidate()
170
    {
171
        if ($this->application->getAttributes()->getSendCarbonCopy()) {
172
            $this->mailService->send(
173
                'Applications/CarbonCopy',
174
                [
175
                    'application' => $this->application
176
                ]
177
            );
178
        }
179
    }
180
}
181