Completed
Push — develop ( e649e8...88dc2a )
by
unknown
15:53 queued 08:30
created

NewApplication   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 23
c 3
b 0
f 2
lcom 1
cbo 9
dl 0
loc 157
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B init() 0 42 6
A setJob() 0 8 2
A setUser() 0 5 1
A setAdmin() 0 5 1
B setReciptient() 0 16 7
A getDepartmentManagers() 0 9 3
A getWorkflowSettings() 0 8 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** NewApplication.php */
11
namespace Applications\Mail;
12
13
use Auth\Entity\UserInterface;
14
use Jobs\Entity\JobInterface;
15
use Core\Mail\StringTemplateMessage;
16
use Organizations\Entity\EmployeeInterface;
17
18
/**
19
 * Sends Information about a new Application to the recruiter
20
 *
21
 * Class NewApplication
22
 * @package Applications\Mail
23
 */
24
class NewApplication extends StringTemplateMessage
25
{
26
    /**
27
     * Job posting
28
     *
29
     * @var \Jobs\Entity\Job $job
30
     */
31
    protected $job;
32
33
    /**
34
     * Owner of the job posting
35
     *
36
     * @var \Auth\Entity\User $user
37
     */
38
    protected $user;
39
40
    /**
41
     * Organization Admin
42
     *
43
     * @var bool|\Auth\Entity\User $admin
44
     */
45
    protected $admin;
46
47
    /**
48
     * @var bool
49
     */
50
    private $callInitOnSetJob = false;
51
52
    /**
53
     * @param array $options
54
     */
55
    public function __construct(array $options = array())
56
    {
57
        parent::__construct($options);
58
        $this->callInitOnSetJob = true;
59
    }
60
    
61
    public function init()
62
    {
63
        if (!$this->job) {
64
            return false;
65
        }
66
67
        /* @var \Auth\Entity\Info $userInfo */
68
        $userInfo = $this->user->getInfo();
69
        $name = $userInfo->getDisplayName();
70
        if ('' == trim($name)) {
71
            $name = $userInfo->getEmail();
72
        }
73
        
74
        $variables = array(
75
            'name' => $name,
76
            'title' => $this->job->getTitle()
77
        );
78
79
        $this->setReciptient();
80
81
        $this->setVariables($variables);
82
        $subject = /*@translate*/ 'New application for your vacancy "%s"';
83
84
        if ($this->isTranslatorEnabled()) {
85
            $subject = $this->getTranslator()->translate($subject);
86
        }
87
        $this->setSubject(sprintf($subject, $this->job->getTitle()));
88
        
89
        /* @var \Applications\Entity\Settings $settings */
90
        $settings = $this->user->getSettings('Applications');
91
92
        $body = $settings->getMailAccessText();
0 ignored issues
show
Documentation Bug introduced by
The method getMailAccessText does not exist on object<Applications\Entity\Settings>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
93
        if ('' == $body) {
94
            $body = /*@translate*/ "Hello ##name##,\n\nThere is a new application for your vacancy:\n\"##title##\"\n\n";
95
            if ($this->isTranslatorEnabled()) {
96
                $body = $this->getTranslator()->translate($body);
97
            }
98
        }
99
        
100
        $this->setBody($body);
101
        return $this;
102
    }
103
104
    /**
105
     * @param JobInterface $job
106
     * @param bool $init
107
     * @return $this
108
     */
109
    public function setJob(JobInterface $job, $init = true)
0 ignored issues
show
Unused Code introduced by
The parameter $init is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        $this->job = $job;
0 ignored issues
show
Documentation Bug introduced by
$job is of type object<Jobs\Entity\JobInterface>, but the property $job was declared to be of type object<Jobs\Entity\Job>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
112
        if ($this->callInitOnSetJob) {
113
            $this->init();
114
        }
115
        return $this;
116
    }
117
118
    /**
119
     * @param \Auth\Entity\User $user
120
     * @return $this
121
     */
122
    public function setUser($user)
123
    {
124
        $this->user=$user;
125
        return $this;
126
    }
127
128
    /**
129
     * @param \Auth\Entity\User $admin
130
     * @return $this
131
     */
132
    public function setAdmin($admin)
133
    {
134
        $this->admin = $admin;
135
        return $this;
136
    }
137
138
    protected function setReciptient() {
139
        $workflowSettings = $this->getWorkflowSettings();
140
        if ($workflowSettings->getAcceptApplicationByDepartmentManager()){
141
            $departmentManagers = $this->getDepartmentManagers();
142
            foreach ($departmentManagers as $employee) { /* @var \Organizations\Entity\Employee $employee */
0 ignored issues
show
Bug introduced by
The expression $departmentManagers of type boolean|object<Doctrine\...Collections\Collection> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
143
                $this->setTo($employee->getUser()->getInfo()->getEmail(), $employee->getUser()->getInfo()->getDisplayName());
144
            }
145
        } else {
146
            $this->setTo($this->user->getInfo()->getEmail(), $this->user->getInfo()->getDisplayName());
147
        }
148
        if (true === $this->admin && $this->user->getSettings('Applications')->getMailBCC()) {
149
            $this->addBcc($this->user->getInfo()->getEmail(), $this->user->getInfo()->getDisplayName());
150
        } elseif($this->admin instanceof UserInterface && $this->admin->getSettings('Applications')->getMailBCC()) {
151
            $this->addBcc($this->admin->getInfo()->getEmail(), $this->admin->getInfo()->getDisplayName());
152
        }
153
    }
154
155
    /**
156
     * @return bool|\Doctrine\Common\Collections\Collection
157
     */
158
    protected function getDepartmentManagers(){
159
        if (true === $this->admin) {
160
            return $this->user->getOrganization()->getOrganization()->getEmployeesByRole(EmployeeInterface::ROLE_DEPARTMENT_MANAGER);
161
        } elseif($this->admin) {
162
            return $this->admin->getOrganization()->getOrganization()->getEmployeesByRole(EmployeeInterface::ROLE_DEPARTMENT_MANAGER);
163
        } else {
164
            return false;
165
        }
166
    }
167
168
    /**
169
     * @return bool|\Organizations\Entity\WorkflowSettings
170
     */
171
    protected function getWorkflowSettings()
172
    {
173
        if ($this->user->getOrganization()->hasAssociation()) {
174
            return $this->user->getOrganization()->getOrganization()->getWorkflowSettings();
175
        } else {
176
            return false;
177
        }
178
    }
179
180
}
181