Passed
Push — master ( a0632e...2294fc )
by Peter
03:03
created

Message::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Contact\Service\Execute;
6
7
use AbterPhp\Contact\Constant\Event;
8
use AbterPhp\Contact\Domain\Entities\Form;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, AbterPhp\Contact\Service\Execute\Form. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use AbterPhp\Contact\Domain\Entities\Message as Entity;
10
use AbterPhp\Contact\Orm\FormRepo;
11
use AbterPhp\Contact\Validation\Factory\Message as ValidatorFactory;
12
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
13
use AbterPhp\Framework\Email\Sender;
14
use Opulence\Events\Dispatchers\IEventDispatcher;
15
use Opulence\Orm\OrmException;
16
17
class Message
18
{
19
    /** @var ValidatorFactory */
20
    protected $validatorFactory;
21
22
    /** @var FormRepo */
23
    protected $formRepo;
24
25
    /** @var Sender */
26
    protected $sender;
27
28
    /** @var IEventDispatcher */
29
    protected $eventDispatcher;
30
31
    /** @var Form[] */
32
    protected $forms = [];
33
34
    /**
35
     * Message constructor.
36
     *
37
     * @param ValidatorFactory $validatorFactory
38
     * @param FormRepo         $formRepo
39
     * @param Sender           $sender
40
     * @param IEventDispatcher $eventDispatcher
41
     */
42
    public function __construct(
43
        ValidatorFactory $validatorFactory,
44
        FormRepo $formRepo,
45
        Sender $sender,
46
        IEventDispatcher $eventDispatcher
47
    ) {
48
        $this->validatorFactory = $validatorFactory;
49
        $this->formRepo         = $formRepo;
50
        $this->sender           = $sender;
51
        $this->eventDispatcher  = $eventDispatcher;
52
    }
53
54
    /**
55
     * @param string $formIdentifier
56
     *
57
     * @return Form|null
58
     * @throws \Opulence\Orm\OrmException
59
     */
60
    public function getForm(string $formIdentifier): ?Form
61
    {
62
        if (array_key_exists($formIdentifier, $this->forms)) {
63
            return $this->form[$formIdentifier];
64
        }
65
66
        try {
67
            $form = $this->formRepo->getByIdentifier($formIdentifier);
68
        } catch (OrmException $e) {
69
            try {
70
                $form = $this->formRepo->getById($formIdentifier);
71
            } catch (OrmException $e) {
72
                $this->form[$formIdentifier] = null;
0 ignored issues
show
Bug Best Practice introduced by
The property form does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
73
74
                return null;
75
            }
76
        }
77
78
        $this->form[$form->getId()]         = $form;
79
        $this->form[$form->getIdentifier()] = $form;
80
81
        return $form;
82
    }
83
84
    /**
85
     * @param Entity $message
86
     *
87
     * @return int
88
     */
89
    public function send(Entity $message): int
90
    {
91
        $this->eventDispatcher->dispatch(Event::MESSAGE_READY, $message);
92
93
        $form       = $message->getForm();
94
        $recipients = [$form->getToEmail() => $form->getToName()];
95
96
        $replyToAddresses = [$message->getFromEmail() => $message->getFromName()];
97
        $fromAddresses    = $replyToAddresses;
98
99
        $result = $this->sender->send(
100
            $message->getSubject(),
101
            $message->getBody(),
102
            $recipients,
103
            $fromAddresses,
104
            $replyToAddresses
105
        );
106
107
        $this->eventDispatcher->dispatch(Event::MESSAGE_SENT, $message);
108
109
        return $result;
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    public function getFailedRecipients(): array
116
    {
117
        return $this->sender->getFailedRecipients();
118
    }
119
120
    /**
121
     * @param string $formIdentifier
122
     * @param array  $postData
123
     *
124
     * @return array errors
125
     */
126
    public function validateForm(string $formIdentifier, array $postData): array
127
    {
128
        /** @var Form $form */
129
        $form = $this->getForm($formIdentifier);
130
        if (null === $form) {
131
            return [];
132
        }
133
134
        $validator = $this->validatorFactory->setMaxBodyLength($form->getMaxBodyLength())->createValidator();
135
136
        if ($validator->isValid($postData)) {
137
            return [];
138
        }
139
140
        return $validator->getErrors()->getAll();
141
    }
142
143
    /**
144
     * @param string $entityId
145
     *
146
     * @return Entity
147
     */
148
    public function createEntity(string $entityId): IStringerEntity
149
    {
150
        $form = new Form('', '', '', '', '', '', '', 0);
151
152
        $message = new Entity($entityId, $form, '', '', '', '');
153
154
        return $message;
155
    }
156
157
    /**
158
     * @param string $formIdentifier
159
     * @param Entity $entity
160
     * @param array  $postData
161
     * @param array  $fileData
162
     *
163
     * @return IStringerEntity
164
     */
165
    public function fillEntity(
166
        string $formIdentifier,
167
        IStringerEntity $entity,
168
        array $postData,
169
        array $fileData
0 ignored issues
show
Unused Code introduced by
The parameter $fileData is not used and could be removed. ( Ignorable by Annotation )

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

169
        /** @scrutinizer ignore-unused */ array $fileData

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

Loading history...
170
    ): IStringerEntity {
171
        $form = $this->getForm($formIdentifier);
172
        if (null === $form) {
173
            return $entity;
174
        }
175
176
        $entity
177
            ->setForm($form)
0 ignored issues
show
Bug introduced by
The method setForm() does not exist on AbterPhp\Framework\Domain\Entities\IStringerEntity. It seems like you code against a sub-type of AbterPhp\Framework\Domain\Entities\IStringerEntity such as AbterPhp\Contact\Domain\Entities\Message. ( Ignorable by Annotation )

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

177
            ->/** @scrutinizer ignore-call */ 
178
              setForm($form)
Loading history...
178
            ->setSubject($postData['subject'])
179
            ->setBody($postData['body'])
180
            ->setFromName($postData['from_name'])
181
            ->setFromEmail($postData['from_email']);
182
183
        return $entity;
184
    }
185
}
186