Completed
Push — middleware-wip ( 71b03c...531d78 )
by Romain
03:32
created

SessionPersistence   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 11 2
A has() 0 6 1
A fetch() 0 9 1
A save() 0 6 1
A delete() 0 6 1
A sanitizeIdentifier() 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\Persistence\Item\Session;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Exceptions\EntryNotFoundException;
18
use Romm\Formz\Form\FormInterface;
19
use Romm\Formz\Form\Service\DataObject\FormIdentifierObject;
20
use Romm\Formz\Persistence\AbstractPersistence;
21
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
22
use TYPO3\CMS\Extbase\Service\EnvironmentService;
23
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
24
25
class SessionPersistence extends AbstractPersistence
26
{
27
    /**
28
     * @var int
29
     */
30
    protected $priority = 500;
31
32
    /**
33
     * @var FrontendUserAuthentication|BackendUserAuthentication
34
     */
35
    protected $user;
36
37
    /**
38
     * Initializes the user session, depending on the current TYPO3 environment.
39
     */
40
    public function initialize()
41
    {
42
        $environmentService = Core::instantiate(EnvironmentService::class);
43
44
        if ($environmentService->isEnvironmentInFrontendMode()) {
45
            $this->user = Core::get()->getPageController()->fe_user;
46
            $this->user->fetchSessionData();
0 ignored issues
show
Bug introduced by
The method fetchSessionData() does not seem to exist on object<TYPO3\CMS\Fronten...tendUserAuthentication>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        } else {
48
            $this->user = Core::get()->getBackendUser();
49
        }
50
    }
51
52
    /**
53
     * Checks that the form instance that matches the identifier exists in the
54
     * session.
55
     *
56
     * @param FormIdentifierObject $identifierObject
57
     * @return bool
58
     */
59
    public function has(FormIdentifierObject $identifierObject)
60
    {
61
        $identifier = $this->sanitizeIdentifier($identifierObject->getIdentifierHash());
62
63
        return $this->user->getSessionData($identifier) !== null;
64
    }
65
66
    /**
67
     * Returns the form instance that matches the identifier. If it does not
68
     * exist, and exception is thrown.
69
     *
70
     * @param FormIdentifierObject $identifierObject
71
     * @return FormInterface
72
     * @throws EntryNotFoundException
73
     */
74
    public function fetch(FormIdentifierObject $identifierObject)
75
    {
76
        $this->checkInstanceCanBeFetched($identifierObject);
77
78
        $identifier = $this->sanitizeIdentifier($identifierObject->getIdentifierHash());
79
        $form = $this->user->getSessionData($identifier);
80
81
        return $form;
82
    }
83
84
    /**
85
     * Adds the given form entry to the session.
86
     *
87
     * @param FormIdentifierObject $identifierObject
88
     * @param FormInterface $form
89
     */
90
    public function save(FormIdentifierObject $identifierObject, FormInterface $form)
91
    {
92
        $identifier = $this->sanitizeIdentifier($identifierObject->getIdentifierHash());
93
94
        $this->user->setAndSaveSessionData($identifier, $form);
95
    }
96
97
    /**
98
     * Removes the given entry from session.
99
     *
100
     * @param FormIdentifierObject $identifierObject
101
     * @return void
102
     */
103
    public function delete(FormIdentifierObject $identifierObject)
104
    {
105
        $identifier = $this->sanitizeIdentifier($identifierObject->getIdentifierHash());
106
107
        $this->user->setAndSaveSessionData($identifier, null);
108
    }
109
110
    /**
111
     * @param string $identifier
112
     * @return string
113
     */
114
    protected function sanitizeIdentifier($identifier)
115
    {
116
        return 'FormZ-' . $identifier;
117
    }
118
}
119