1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* YAWIK |
5
|
|
|
* |
6
|
|
|
* @filesource |
7
|
|
|
* @copyright 2020 CROSS Solution <https://www.cross-solution.de> |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Applications\Controller; |
14
|
|
|
|
15
|
|
|
use Applications\Entity\Contact; |
16
|
|
|
use Applications\Entity\Hydrator\ApiApplicationHydrator; |
17
|
|
|
use Applications\Repository\Application as ApplicationsRepository; |
18
|
|
|
use Auth\Entity\AnonymousUser; |
19
|
|
|
use Jobs\Repository\Job as JobsRepository; |
20
|
|
|
use Laminas\Mvc\Controller\AbstractActionController; |
21
|
|
|
use Laminas\View\Model\JsonModel; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* TODO: description |
25
|
|
|
* |
26
|
|
|
* @author Mathias Gelhausen <[email protected]> |
27
|
|
|
* TODO: write tests |
28
|
|
|
*/ |
29
|
|
|
class ApiApplyController extends AbstractActionController |
30
|
|
|
{ |
31
|
|
|
private $appRepository; |
32
|
|
|
private $jobRepository; |
33
|
|
|
private $formContainer; |
34
|
|
|
private $hydrator; |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
ApplicationsRepository $appRepository, |
38
|
|
|
JobsRepository $jobRepository, |
39
|
|
|
$formContainer, |
40
|
|
|
ApiApplicationHydrator $hydrator |
41
|
|
|
) { |
42
|
|
|
$this->appRepository = $appRepository; |
43
|
|
|
$this->jobRepository = $jobRepository; |
44
|
|
|
$this->formContainer = $formContainer; |
45
|
|
|
$this->hydrator = $hydrator; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function indexAction() |
49
|
|
|
{ |
50
|
|
|
/** @var \Laminas\Http\PhpEnvironment\Request $request */ |
51
|
|
|
$request = $this->getRequest(); |
52
|
|
|
if (!$request->isPost()) { |
53
|
|
|
return $this->noPostRequestModel(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$applyId = $this->params()->fromQuery('applyId'); |
57
|
|
|
$job = $this->jobRepository->findOneBy(['applyId' => $applyId]); |
58
|
|
|
|
59
|
|
|
if (!$job) { |
|
|
|
|
60
|
|
|
return $this->noJobFoundModel($applyId); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$data = array_merge_recursive( |
64
|
|
|
$request->getPost()->toArray(), |
65
|
|
|
$request->getFiles()->toArray() |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
$application = $this->appRepository->create(); |
70
|
|
|
$application->setJob($job); |
71
|
|
|
$user = $this->auth()->getUser(); |
|
|
|
|
72
|
|
|
$application->setUser($user); |
73
|
|
|
$application->setContact(new Contact()); |
74
|
|
|
|
75
|
|
|
$hydrator = $this->hydrator; |
76
|
|
|
$hydrator->hydrate($data, $application); |
77
|
|
|
//$application->getContact()->setFirstName($data['contact']['firstName']); |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
$this->appRepository->store($application); |
81
|
|
|
|
82
|
|
|
$result = [ |
83
|
|
|
'status' => 'OK', |
84
|
|
|
'id' => $application->getId(), |
85
|
|
|
'entity' => $hydrator->extract($application) |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
if ($user instanceof AnonymousUser) { |
89
|
|
|
$result['track'] = |
90
|
|
|
$this->url()->fromRoute( |
91
|
|
|
'lang/applications/detail', |
92
|
|
|
['id' => $application->getId()], |
93
|
|
|
['force_canonical' => true], |
94
|
|
|
true |
95
|
|
|
) |
96
|
|
|
. '?token=' . $user->getToken() |
97
|
|
|
; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$model = new JsonModel($result); |
101
|
|
|
return $model; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function noPostRequestModel() |
105
|
|
|
{ |
106
|
|
|
/** @var \Laminas\Http\PhpEnvironment\Response $response */ |
107
|
|
|
$response = $this->getResponse(); |
108
|
|
|
$response->setStatusCode($response::STATUS_CODE_405); |
109
|
|
|
|
110
|
|
|
return new JsonModel([ |
111
|
|
|
'status' => 'error', |
112
|
|
|
'message' => 'Invalid request method. Only POST allowed.' |
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function invalidDataModel($e) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
/** @var \Laminas\Http\PhpEnvironment\Response $response */ |
119
|
|
|
$response = $this->getResponse(); |
120
|
|
|
$response->setStatusCode($response::STATUS_CODE_405); |
121
|
|
|
|
122
|
|
|
return new JsonModel([ |
123
|
|
|
'status' => 'error', |
124
|
|
|
'message' => 'Invalid json data: ' . $e->getMessage() |
125
|
|
|
]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function noJobFoundModel($applyId) |
129
|
|
|
{ |
130
|
|
|
/** @var \Laminas\Http\PhpEnvironment\Response $response */ |
131
|
|
|
$response = $this->getResponse(); |
132
|
|
|
$response->setStatusCode($response::STATUS_CODE_400); |
133
|
|
|
|
134
|
|
|
return new JsonModel([ |
135
|
|
|
'status' => 'error', |
136
|
|
|
'message' => 'No job found with apply id "' . $applyId . '"', |
137
|
|
|
]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function invalidApplicationDataModel() |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
/** @var \Laminas\Http\PhpEnvironment\Response $response */ |
143
|
|
|
$response = $this->getResponse(); |
144
|
|
|
$response->setStatusCode($response::STATUS_CODE_400); |
145
|
|
|
|
146
|
|
|
return new JsonModel([ |
147
|
|
|
'status' => 'error', |
148
|
|
|
'message' => 'Invalid application data.', |
149
|
|
|
'errrors' => $this->formContainer->getMessages() |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|