Completed
Push — middleware-wip ( ffd957...48fd00 )
by Romain
02:53
created

FormObjectMetadata   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getMetadata() 0 22 4
A resetMetadata() 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\FormObject\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
    public function resetMetadata()
83
    {
84
        $this->metadata = null;
85
    }
86
87
    /**
88
     * @return int|string
89
     */
90
    protected function getFormIdentifier()
91
    {
92
        $identifier = null;
93
94
        if ($this->formObject->hasForm()) {
95
            $form = $this->formObject->getForm();
96
97
            if ($form instanceof IdentifiableFormInterface) {
98
                $identifier = $form->getFormIdentifier();
99
            } elseif ($form instanceof DomainObjectInterface
100
                && null !== $form->getUid()
101
            ) {
102
                $identifier = $form->getUid();
103
            }
104
        }
105
106
        return $identifier;
107
    }
108
109
    /**
110
     * @param FormMetadataRepository $metadataRepository
111
     */
112
    public function injectMetadataRepository(FormMetadataRepository $metadataRepository)
113
    {
114
        $this->metadataRepository = $metadataRepository;
115
    }
116
}
117