Completed
Pull Request — master (#588)
by Mathias
07:35
created

ApiApplyController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 10
cc 1
nc 1
nop 3
crap 2
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) {
0 ignored issues
show
introduced by
$job is of type object, thus it always evaluated to true.
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method auth() does not exist on Applications\Controller\ApiApplyController. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        $user = $this->/** @scrutinizer ignore-call */ auth()->getUser();
Loading history...
Bug introduced by
The method getUser() does not exist on Laminas\Stdlib\DispatchableInterface. It seems like you code against a sub-type of Laminas\Stdlib\DispatchableInterface such as Laminas\Mvc\Controller\AbstractController. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        $user = $this->auth()->/** @scrutinizer ignore-call */ getUser();
Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The method invalidDataModel() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
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()
0 ignored issues
show
Unused Code introduced by
The method invalidApplicationDataModel() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
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