|
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 Laminas\Json\Json; |
|
18
|
|
|
use Laminas\Mvc\Controller\AbstractActionController; |
|
19
|
|
|
use Laminas\View\Model\JsonModel; |
|
20
|
|
|
use Applications\Repository\Application as ApplicationsRepository; |
|
21
|
|
|
use Jobs\Repository\Job as JobsRepository; |
|
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
|
|
|
|
|
35
|
|
|
public function __construct( |
|
36
|
|
|
ApplicationsRepository $appRepository, |
|
37
|
|
|
JobsRepository $jobRepository, |
|
38
|
|
|
$formContainer |
|
39
|
|
|
) { |
|
40
|
|
|
$this->appRepository = $appRepository; |
|
41
|
|
|
$this->jobRepository = $jobRepository; |
|
42
|
|
|
$this->formContainer = $formContainer; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function indexAction() |
|
46
|
|
|
{ |
|
47
|
|
|
/** @var \Laminas\Http\PhpEnvironment\Request $request */ |
|
48
|
|
|
$request = $this->getRequest(); |
|
49
|
|
|
if (!$request->isPost()) { |
|
50
|
|
|
return $this->noPostRequestModel(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$applyId = $this->params()->fromQuery('applyId'); |
|
54
|
|
|
$job = $this->jobRepository->findOneBy(['applyId' => $applyId]); |
|
55
|
|
|
|
|
56
|
|
|
if (!$job) { |
|
|
|
|
|
|
57
|
|
|
return $this->noJobFoundModel($applyId); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$data = array_merge_recursive( |
|
61
|
|
|
$request->getPost()->toArray(), |
|
62
|
|
|
$request->getFiles()->toArray() |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$application = $this->appRepository->create(); |
|
67
|
|
|
$application->setJob($job); |
|
68
|
|
|
$user = $this->auth()->getUser(); |
|
|
|
|
|
|
69
|
|
|
$application->setUser($user); |
|
70
|
|
|
$application->setContact(new Contact()); |
|
71
|
|
|
|
|
72
|
|
|
$hydrator = new ApiApplicationHydrator(); |
|
73
|
|
|
$hydrator->hydrate($data, $application); |
|
74
|
|
|
//$application->getContact()->setFirstName($data['contact']['firstName']); |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
$result = [ |
|
78
|
|
|
'status' => 'OK', |
|
79
|
|
|
'id' => $application->getId(), |
|
80
|
|
|
'test' => $application->getContact()->getFirstName(), |
|
81
|
|
|
'test2' => $application->getJob()->getId(), |
|
82
|
|
|
'entity' => $hydrator->extract($application) |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
$model = new JsonModel($result); |
|
86
|
|
|
return $model; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function noPostRequestModel() |
|
90
|
|
|
{ |
|
91
|
|
|
/** @var \Laminas\Http\PhpEnvironment\Response $response */ |
|
92
|
|
|
$response = $this->getResponse(); |
|
93
|
|
|
$response->setStatusCode($response::STATUS_CODE_405); |
|
94
|
|
|
|
|
95
|
|
|
return new JsonModel([ |
|
96
|
|
|
'status' => 'error', |
|
97
|
|
|
'message' => 'Invalid request method. Only POST allowed.' |
|
98
|
|
|
]); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function invalidDataModel($e) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
/** @var \Laminas\Http\PhpEnvironment\Response $response */ |
|
104
|
|
|
$response = $this->getResponse(); |
|
105
|
|
|
$response->setStatusCode($response::STATUS_CODE_405); |
|
106
|
|
|
|
|
107
|
|
|
return new JsonModel([ |
|
108
|
|
|
'status' => 'error', |
|
109
|
|
|
'message' => 'Invalid json data: ' . $e->getMessage() |
|
110
|
|
|
]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
private function noJobFoundModel($applyId) |
|
114
|
|
|
{ |
|
115
|
|
|
/** @var \Laminas\Http\PhpEnvironment\Response $response */ |
|
116
|
|
|
$response = $this->getResponse(); |
|
117
|
|
|
$response->setStatusCode($response::STATUS_CODE_400); |
|
118
|
|
|
|
|
119
|
|
|
return new JsonModel([ |
|
120
|
|
|
'status' => 'error', |
|
121
|
|
|
'message' => 'No job found with apply id "' . $applyId . '"', |
|
122
|
|
|
]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
private function invalidApplicationDataModel() |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
/** @var \Laminas\Http\PhpEnvironment\Response $response */ |
|
128
|
|
|
$response = $this->getResponse(); |
|
129
|
|
|
$response->setStatusCode($response::STATUS_CODE_400); |
|
130
|
|
|
|
|
131
|
|
|
return new JsonModel([ |
|
132
|
|
|
'status' => 'error', |
|
133
|
|
|
'message' => 'Invalid application data.', |
|
134
|
|
|
'errrors' => $this->formContainer->getMessages() |
|
135
|
|
|
]); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|