Completed
Push — develop ( ad153c...aedce0 )
by
unknown
07:14
created

NewApplication::init()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 49
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 29
nc 13
nop 0
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 Applications\Entity\ApplicationInterface;
14
use Auth\Entity\UserInterface;
15
use Core\Exception\MissingDependencyException;
16
use Jobs\Entity\JobInterface;
17
use Core\Mail\StringTemplateMessage;
18
use Organizations\Entity\EmployeeInterface;
19
use Zend\Mvc\Router\RouteStackInterface;
20
21
/**
22
 * Sends Information about a new Application to the recruiter
23
 *
24
 * Class NewApplication
25
 * @package Applications\Mail
26
 */
27
class NewApplication extends StringTemplateMessage
28
{
29
    /**
30
     * Job posting
31
     *
32
     * @var ApplicationInterface
33
     */
34
    protected $application;
35
36
    /**
37
     * Owner of the job posting
38
     *
39
     * @var \Auth\Entity\User $user
40
     */
41
    protected $user;
42
43
    /**
44
     * Organization Admin
45
     *
46
     * @var bool|\Auth\Entity\User $admin
47
     */
48
    protected $admin;
49
50
    /**
51
     * @var bool
52
     */
53
    private $callInitOnSetJob = false;
54
55
    /**
56
     *
57
     *
58
     * @var RouteStackInterface
59
     */
60
    private $router;
61
62
    /**
63
     * @param array $options
64
     */
65
    public function __construct($options = array())
66
    {
67
        if (!is_array($options)) {
68
            $this->router = $options;
69
            $options = [];
70 View Code Duplication
        } else if (isset($options['router'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
            $this->router = $options['router'];
72
            unset($options['router']);
73
        }
74
75
        if (!$this->router) {
76
            throw new MissingDependencyException('Router', $this);
77
        }
78
79
        parent::__construct($options);
80
        $this->callInitOnSetJob = true;
81
    }
82
83
    public function init()
84
    {
85
        if (!$this->application) {
86
            return false;
87
        }
88
89
        /* @var \Auth\Entity\Info $userInfo */
90
        $job = $this->application->getJob();
91
        $userInfo = $this->user->getInfo();
92
        $name = $userInfo->getDisplayName();
93
        if ('' == trim($name)) {
94
            $name = $userInfo->getEmail();
95
        }
96
97
        $variables = [
98
            'name' => $name,
99
            'title' => $job->getTitle(),
100
            'link'  => $this->router->assemble(
101
                            ['id' => $this->application->getId()],
102
                            ['name'=>'lang/applications/detail', 'force_canonical'=>true]
103
                       ),
104
        ];
105
106
        $this->setTo($this->user->getInfo()->getEmail(), $this->user->getInfo()->getDisplayName(false));
107
108
        $this->setVariables($variables);
109
        $subject = /*@translate*/ 'New application for your vacancy "%s"';
110
111
        if ($this->isTranslatorEnabled()) {
112
            $subject = $this->getTranslator()->translate($subject);
113
        }
114
        $this->setSubject(sprintf($subject, $job->getTitle()));
115
        
116
        /* @var \Applications\Entity\Settings $settings */
117
        $settings = $this->user->getSettings('Applications');
118
119
        $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...
120
        if ('' == $body) {
121
            $body = /*@translate*/ "Hello ##name##,\n\nThere is a new application for your vacancy:\n\"##title##\"\n\n";
122
123
            if ($this->isTranslatorEnabled()) {
124
                $body = $this->getTranslator()->translate($body);
125
            }
126
            $body .= "##link##\n\n";
127
        }
128
        
129
        $this->setBody($body);
130
        return $this;
131
    }
132
133
    /**
134
     * @param ApplicationInterface $application
135
     * @param bool $init
136
     * @return $this
137
     */
138
    public function setApplication(ApplicationInterface $application, $init = true)
139
    {
140
        $this->application = $application;
141
        if ($this->callInitOnSetApplication) {
0 ignored issues
show
Bug introduced by
The property callInitOnSetApplication does not seem to exist. Did you mean application?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
142
            $this->init();
143
        }
144
        return $this;
145
    }
146
147
    /**
148
     * @param \Auth\Entity\User $user
149
     * @return $this
150
     */
151
    public function setUser($user)
152
    {
153
        $this->user=$user;
154
        return $this;
155
    }
156
}
157