Completed
Push — master ( 0913ed...15a2b1 )
by Mathias
21s queued 15s
created

ApiApplyController::indexAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 54
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 32
c 2
b 0
f 1
dl 0
loc 54
ccs 0
cts 41
cp 0
rs 9.408
cc 4
nc 4
nop 0
crap 20

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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) {
0 ignored issues
show
introduced by
$job is of type object, thus it always evaluated to true.
Loading history...
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();
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

71
        $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

71
        $user = $this->auth()->/** @scrutinizer ignore-call */ getUser();
Loading history...
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)
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...
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()
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...
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