Completed
Push — master ( 4ad177...ddee2e )
by Mathias
33s queued 17s
created

StatusChange   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
eloc 44
dl 0
loc 124
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setVariablesFromApplication() 0 9 1
A getJobTitle() 0 3 1
A getFormalSalutation() 0 12 2
A setApplication() 0 6 1
A getInformalSalutation() 0 9 1
A __construct() 0 4 1
A getDate() 0 5 1
A getApplicationLink() 0 10 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright https://yawik.org/COPYRIGHT.php
7
 * @license   MIT
8
 */
9
10
/** NewApplication.php */
11
namespace Applications\Mail;
12
13
use Core\Mail\StringTemplateMessage;
14
use Applications\Entity\ApplicationInterface;
15
use Auth\Entity\AnonymousUser;
16
17
class StatusChange extends StringTemplateMessage implements StatusChangeInterface
18
{
19
    /**
20
     * @var ApplicationInterface
21
     */
22
    protected $application;
23
24
    /**
25
     * placeholders, which are replaced in the mail
26
     *
27
     * @var array
28
     */
29
    protected $callbacks = array(
30
        'anrede_formell' => 'getFormalSalutation',
31
        'salutation_formal' => 'getFormalSalutation',
32
        'anrede_informell' => 'getInformalSalutation',
33
        'salutation_informal' => 'getInformalSalutation',
34
        'job_title' => 'getJobTitle',
35
        'date' => 'getDate',
36
        'link' => 'getApplicationLink',
37
    );
38
39
    public function __construct($router, array $options = [])
40
    {
41
        $this->router = $router;
0 ignored issues
show
Bug Best Practice introduced by
The property router does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42
        parent::__construct($options);
43
    }
44
    /**
45
     * @param ApplicationInterface $application
46
     * @return StringTemplateMessage
47
     */
48
    public function setVariablesFromApplication(ApplicationInterface $application)
49
    {
50
        $contact = $application->getContact();
51
        $name    = $contact->getDisplayName();
52
53
        $variables = array(
54
            'name' => $name,
55
        );
56
        return $this->setVariables($variables);
57
    }
58
59
    /**
60
     * Sets the application
61
     *
62
     * @param ApplicationInterface $application
63
     * @param string|null $status
64
     * @return $this
65
     */
66
    public function setApplication(ApplicationInterface $application, $status = null)
67
    {
68
        $this->application = $application;
69
        $this->setTo($application->getContact()->getEmail(), $application->getContact()->getDisplayName(false));
70
        $this->setVariablesFromApplication($application);
71
        return $this;
72
    }
73
74
    /**
75
     * Gets the formal salutation of the applicant
76
     *
77
     * @return string
78
     */
79
    protected function getFormalSalutation()
80
    {
81
        $contact = $this->application->getContact();
82
        $name    = $contact->getLastName();
83
        $gender  = $contact->getGender();
84
        $translator = $this->getTranslator();
85
86
        $salutation = 'male' == $gender
87
                    ? $translator->translate('Dear Mr. %s')
88
                    : $translator->translate('Dear Ms. %s');
89
90
        return sprintf($salutation, $name);
91
    }
92
93
    /**
94
     * Gets the informal salutation of the applicant
95
     *
96
     * @return string
97
     */
98
    protected function getInformalSalutation()
99
    {
100
        $contact = $this->application->getContact();
101
        $name    = $contact->getDisplayName(false);
102
103
        $salutation = $this->getTranslator()
104
                    ->translate('Hello %s');
105
106
        return sprintf($salutation, $name);
107
    }
108
109
    /**
110
     * Gets the title of the job posting
111
     *
112
     * @return mixed
113
     */
114
    protected function getJobTitle()
115
    {
116
        return $this->application->getJob()->getTitle();
117
    }
118
119
    /**
120
     * Gets the creation date of the application
121
     *
122
     * @return string
123
     */
124
    protected function getDate()
125
    {
126
        /** @var $date \DateTime */
127
        $date = $this->application->getDateCreated();
128
        return strftime('%x', $date->getTimestamp());
129
    }
130
131
    protected function getApplicationLink()
132
    {
133
        $user = $this->application->getUser();
134
        $token = $user instanceof AnonymousUser ? '?token=' . $user->getToken() : '';
135
        $href = $this->router->assemble(
136
            ['id' => $this->application->getId()],
137
            ['name' => 'lang/applications/detail', 'force_canonical' => true]
138
        ) . $token;
139
140
        return $href;
141
    }
142
}
143