Form::createEntity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Contact\Service\Execute;
6
7
use AbterPhp\Admin\Service\Execute\RepoServiceAbstract;
8
use AbterPhp\Contact\Domain\Entities\Form as Entity;
9
use AbterPhp\Contact\Orm\FormRepo as GridRepo;
10
use AbterPhp\Contact\Validation\Factory\Form as ValidatorFactory;
11
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
12
use Cocur\Slugify\Slugify;
13
use Opulence\Events\Dispatchers\IEventDispatcher;
14
use Opulence\Http\Requests\UploadedFile;
15
use Opulence\Orm\IUnitOfWork;
16
use Opulence\Orm\OrmException;
17
18
class Form extends RepoServiceAbstract
19
{
20
    /** @var Slugify */
21
    protected $slugify;
22
23
    /**
24
     * Form constructor.
25
     *
26
     * @param GridRepo         $repo
27
     * @param ValidatorFactory $validatorFactory
28
     * @param IUnitOfWork      $unitOfWork
29
     * @param IEventDispatcher $eventDispatcher
30
     * @param Slugify          $slugify
31
     */
32
    public function __construct(
33
        GridRepo $repo,
34
        ValidatorFactory $validatorFactory,
35
        IUnitOfWork $unitOfWork,
36
        IEventDispatcher $eventDispatcher,
37
        Slugify $slugify
38
    ) {
39
        parent::__construct($repo, $validatorFactory, $unitOfWork, $eventDispatcher);
40
41
        $this->slugify = $slugify;
42
    }
43
44
    /**
45
     * @param string $entityId
46
     *
47
     * @return Entity
48
     */
49
    public function createEntity(string $entityId): IStringerEntity
50
    {
51
        return new Entity($entityId, '', '', '', '', '', '', 0);
52
    }
53
54
    /**
55
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
56
     *
57
     * @param IStringerEntity $entity
58
     * @param array           $postData
59
     * @param UploadedFile[]  $fileData
60
     *
61
     * @return Entity
62
     * @throws OrmException
63
     */
64
    protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity
65
    {
66
        assert($entity instanceof Entity, new \InvalidArgumentException('Invalid entity'));
67
68
        $name = $postData['name'];
69
70
        $identifier = $postData['identifier'] ?? $entity->getIdentifier();
71
        $identifier = $identifier ?: $name;
72
        $identifier = $this->slugify->slugify($identifier);
73
74
        $toName        = $postData['to_name'];
75
        $toEmail       = $postData['to_email'];
76
        $successUrl    = $postData['success_url'];
77
        $failureUrl    = $postData['failure_url'];
78
        $maxBodyLength = $postData['max_body_length'];
79
80
        $identifier = $this->slugify->slugify($identifier);
81
82
        $entity
83
            ->setName($name)
84
            ->setIdentifier($identifier)
85
            ->setToName($toName)
86
            ->setToEmail($toEmail)
87
            ->setSuccessUrl($successUrl)
88
            ->setFailureUrl($failureUrl)
89
            ->setMaxBodyLength((int)$maxBodyLength);
90
91
        return $entity;
92
    }
93
}
94