Completed
Push — middleware-wip ( 1bcdac...ffd957 )
by Romain
02:46
created

FormObjectMetadata   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 89
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getMetadata() 0 22 4
A setMetadata() 0 4 1
B getFormIdentifier() 0 18 5
A injectMetadataRepository() 0 4 1
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Form\Service;
15
16
use Romm\Formz\Domain\Model\DataObject\FormMetadataObject;
17
use Romm\Formz\Domain\Model\FormMetadata;
18
use Romm\Formz\Domain\Repository\FormMetadataRepository;
19
use Romm\Formz\Form\FormObject\FormObject;
20
use Romm\Formz\Form\IdentifiableFormInterface;
21
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
22
23
class FormObjectMetadata
24
{
25
    /**
26
     * @var FormObject
27
     */
28
    protected $formObject;
29
30
    /**
31
     * @var FormMetadata
32
     */
33
    protected $metadata;
34
35
    /**
36
     * @var FormMetadataRepository
37
     */
38
    protected $metadataRepository;
39
40
    /**
41
     * @param FormObject $formObject
42
     */
43
    public function __construct(FormObject $formObject)
44
    {
45
        $this->formObject = $formObject;
46
    }
47
48
    /**
49
     * @return FormMetadata
50
     */
51
    public function getMetadata()
52
    {
53
        if (null === $this->metadata) {
54
            $formHash = $this->formObject->getFormHash();
55
            $identifier = $this->getFormIdentifier();
56
57
            $this->metadata = ($identifier)
58
                ? $this->metadataRepository->findOneByClassNameAndIdentifier($this->formObject->getClassName(), $identifier)
59
                : $this->metadataRepository->findOneByHash($formHash);
60
61
            if (false === $this->metadata instanceof FormMetadata) {
62
                $this->metadata = new FormMetadata(
63
                    $formHash,
64
                    $this->formObject->getClassName(),
65
                    $this->getFormIdentifier(),
66
                    new FormMetadataObject
67
                );
68
            }
69
        }
70
71
        return $this->metadata;
72
    }
73
74
    /**
75
     * @param FormMetadata $metadata
76
     */
77
    public function setMetadata(FormMetadata $metadata)
78
    {
79
        $this->metadata = $metadata;
80
    }
81
82
    /**
83
     * @return int|string
84
     */
85
    protected function getFormIdentifier()
86
    {
87
        $identifier = null;
88
89
        if ($this->formObject->hasForm()) {
90
            $form = $this->formObject->getForm();
91
92
            if ($form instanceof IdentifiableFormInterface) {
93
                $identifier = $form->getFormIdentifier();
94
            } elseif ($form instanceof DomainObjectInterface
95
                && null !== $form->getUid()
96
            ) {
97
                $identifier = $form->getUid();
98
            }
99
        }
100
101
        return $identifier;
102
    }
103
104
    /**
105
     * @param FormMetadataRepository $metadataRepository
106
     */
107
    public function injectMetadataRepository(FormMetadataRepository $metadataRepository)
108
    {
109
        $this->metadataRepository = $metadataRepository;
110
    }
111
}
112