NewReplyFormHandler::doProcess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the MilioooMessageBundle package.
5
 *
6
 * (c) Michiel boeckaert <[email protected]>
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Miliooo\Messaging\Form\FormHandler;
12
13
use Symfony\Component\Form\FormInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
use Miliooo\Messaging\Form\FormModelProcessor\NewReplyFormProcessorInterface;
16
use Miliooo\Messaging\Form\FormModel\ReplyMessageInterface;
17
18
/**
19
 * Form handler for new replies.
20
 *
21
 * @author Michiel Boeckaert <[email protected]>
22
 */
23
class NewReplyFormHandler extends AbstractFormHandler
24
{
25
    /**
26
     * A reply form processor
27
     * @var NewReplyFormProcessorInterface
28
     */
29
    protected $replyFormProcessor;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param Request                        $request            The request the form will process
35
     * @param NewReplyFormProcessorInterface $replyFormProcessor A reply form model processor
36
     */
37
    public function __construct(Request $request, NewReplyFormProcessorInterface $replyFormProcessor)
38
    {
39
        parent::__construct($request);
40
        $this->replyFormProcessor = $replyFormProcessor;
41
    }
42
43
    /**
44
     * Processes the form with the request
45
     *
46
     * @param FormInterface $form A form instance
47
     *
48
     */
49
    public function doProcess(FormInterface $form)
50
    {
51
        $replyThreadFormModel = $this->getFormData($form);
52
        $replyThreadFormModel->setCreatedAt(new \DateTime('now'));
53
        $this->processFormModelExtra($replyThreadFormModel);
54
        $this->replyFormProcessor->process($replyThreadFormModel);
55
    }
56
57
    /**
58
     * Gets the form data
59
     *
60
     * Helper function to get auto completion
61
     *
62
     * @param FormInterface $form
63
     *
64
     * @return ReplyMessageInterface
65
     *
66
     * @throws \InvalidArgumentException when wrong form data
67
     */
68
    protected function getFormData(FormInterface $form)
69
    {
70
        $data = $form->getData();
71
72
        if (!$data instanceof ReplyMessageInterface) {
73
            throw new \InvalidArgumentException('Form data needs to implement ReplyMessageInterface');
74
        }
75
76
        return $data;
77
    }
78
79
    /**
80
     * Helper function if you need to extend this class.
81
     *
82
     * Here you have your custom form model which implements the NewThreadInterface.
83
     * If you need extra processing of this valid form model you can extend this class and overwrite this function.
84
     *
85
     * An example...
86
     * You want to store the ip address from the client.
87
     * $newThreadFormModel->setIpAddress = $this->request->getClientIp();
88
     *
89
     * @param ReplyMessageInterface $replyThreadFormModel
90
     */
91
    protected function processFormModelExtra(ReplyMessageInterface $replyThreadFormModel)
0 ignored issues
show
Unused Code introduced by
The parameter $replyThreadFormModel is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
    {
93
94
    }
95
}
96