NewThreadMessageFormFactory::maybeSetRecipient()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
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\FormFactory;
12
13
use Miliooo\Messaging\Form\FormModel\NewThreadInterface;
14
use Miliooo\Messaging\User\ParticipantInterface;
15
use Symfony\Component\Form\AbstractType;
16
use Symfony\Component\Form\Form;
17
use Symfony\Component\Form\FormFactoryInterface;
18
use Symfony\Component\Form\DataTransformerInterface;
19
use Symfony\Component\Form\Exception\TransformationFailedException;
20
21
/**
22
 * The form factory for new thread messages
23
 *
24
 * @author Michiel Boeckaert <[email protected]>
25
 */
26
class NewThreadMessageFormFactory extends AbstractMessageFormFactory
27
{
28
    protected $transformer;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param FormFactoryInterface $formFactory    A form factory instance
34
     * @param AbstractType         $formType       The form type
35
     * @param string               $formName       Name of the form
36
     * @param string               $modelClassName FQCN of the form model
37
     * @param DataTransformerInterface $transformer An user to username transformer
38
     */
39
    public function __construct(
40
        FormFactoryInterface $formFactory,
41
        AbstractType $formType,
42
        $formName,
43
        $modelClassName,
44
        DataTransformerInterface $transformer
45
    ) {
46
        $this->transformer = $transformer;
47
48
        parent::__construct($formFactory, $formType, $formName, $modelClassName);
49
    }
50
51
    /**
52
     * Creates a new thread form from a form type with a form model set.
53
     *
54
     * @param ParticipantInterface $sender    The sender
55
     * @param string               $recipient A recipient
56
     *
57
     * @return Form
58
     */
59
    public function create(ParticipantInterface $sender, $recipient = "")
60
    {
61
        $formModel = $this->createNewFormModel();
62
        $formModel->setSender($sender);
63
64
        if ($recipient) {
65
            $this->maybeSetRecipient($recipient, $formModel);
66
        }
67
68
        return $this->formFactory->createNamed($this->formName, $this->formType, $formModel);
69
    }
70
71
    /**
72
     * Creates a new form model object from the modelClassName
73
     *
74
     * @return NewThreadInterface
75
     */
76
    protected function createNewFormModel()
77
    {
78
        return new $this->modelClassName;
79
    }
80
81
    /**
82
     * Maybe sets a recipient
83
     * @param string $recipient
84
     * @param $formModel
85
     */
86
    protected function maybeSetRecipient($recipient, $formModel)
87
    {
88
        try {
89
            $recipient = $this->transformer->reverseTransform($recipient);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $recipient. This often makes code more readable.
Loading history...
90
        } catch (TransformationFailedException $e) {
91
            return;
92
        }
93
94
95
        if ($recipient instanceof ParticipantInterface) {
96
            $formModel->setRecipient($recipient);
97
        }
98
    }
99
}
100