Completed
Push — middleware-wip ( 8fd059...e9d8ee )
by Romain
03:01
created

SessionPersistence   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A has() 0 6 1
A fetch() 0 15 3
A save() 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;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Form\FormInterface;
18
use Romm\Formz\Form\Service\DataObject\FormIdentifierObject;
19
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20
use TYPO3\CMS\Core\SingletonInterface;
21
use TYPO3\CMS\Extbase\Service\EnvironmentService;
22
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
23
24
class SessionPersistence implements PersistenceInterface, SingletonInterface
25
{
26
    /**
27
     * @var EnvironmentService
28
     */
29
    protected $environmentService;
30
31
    /**
32
     * @var FrontendUserAuthentication|BackendUserAuthentication
33
     */
34
    protected $user;
35
36
    /**
37
     * @param EnvironmentService $environmentService
38
     */
39
    public function __construct(EnvironmentService $environmentService)
40
    {
41
        $this->environmentService = $environmentService;
42
43
        if ($this->environmentService->isEnvironmentInFrontendMode()) {
44
            $this->user = Core::get()->getPageController()->fe_user;
45
            $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...
46
        } else {
47
            $this->user = Core::get()->getBackendUser();
48
        }
49
    }
50
51
    /**
52
     * @inheritdoc
53
     *
54
     * @param FormIdentifierObject $identifierObject
55
     * @return bool
56
     */
57
    public function has(FormIdentifierObject $identifierObject)
58
    {
59
        $identifier = $this->sanitizeIdentifier($identifierObject->getIdentifierHash());
60
61
        return $this->user->getSessionData($identifier) !== null;
62
    }
63
64
    /**
65
     * @inheritdoc
66
     *
67
     * @param FormIdentifierObject $identifierObject
68
     * @return FormInterface
69
     */
70
    public function fetch(FormIdentifierObject $identifierObject)
71
    {
72
        if (false === $this->has($identifierObject)) {
73
            throw new \Exception('todo'); // @todo
74
        }
75
76
        $identifier = $this->sanitizeIdentifier($identifierObject->getIdentifierHash());
77
        $form = $this->user->getSessionData($identifier);
78
79
        if (false === $form instanceof FormInterface) {
80
            throw new \Exception('todo'); // @todo
81
        }
82
83
        return $form;
84
    }
85
86
    /**
87
     * @inheritdoc
88
     *
89
     * @param FormIdentifierObject $identifierObject
90
     * @param FormInterface $form
91
     */
92
    public function save(FormIdentifierObject $identifierObject, FormInterface $form)
93
    {
94
        $identifier = $this->sanitizeIdentifier($identifierObject->getIdentifierHash());
95
96
        $this->user->setAndSaveSessionData($identifier, $form);
97
    }
98
99
    /**
100
     * @param string $identifier
101
     * @return string
102
     */
103
    protected function sanitizeIdentifier($identifier)
104
    {
105
        return 'FormZ-' . $identifier;
106
    }
107
}
108