Passed
Push — master ( 791909...fe60d3 )
by Timo
52s
created

ModuleDataStorageService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 61
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadModuleData() 0 13 3
A persistModuleData() 0 4 1
A unsetModuleDataIfCanNotBeSerialized() 0 11 4
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Mvc\Backend\Service;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2013-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\System\Mvc\Backend\ModuleData;
28
use TYPO3\CMS\Core\SingletonInterface;
29
30
/**
31
 * Module data storage service. Used to store and retrieve module state (eg.
32
 * checkboxes, selections).
33
 */
34
class ModuleDataStorageService implements SingletonInterface
35
{
36
37
    /**
38
     * @var string
39
     */
40
    const KEY = 'tx_solr';
41
42
    /**
43
     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
44
     * @inject
45
     */
46
    protected $objectManager;
47
48
    /**
49
     * Loads module data for user settings or returns a fresh object initially
50
     *
51
     * @return ModuleData
52
     */
53
    public function loadModuleData()
54
    {
55
        $moduleData = $GLOBALS['BE_USER']->getModuleData(self::KEY);
56
57
        $this->unsetModuleDataIfCanNotBeSerialized($moduleData);
58
        if (empty($moduleData) || !$moduleData) {
59
            $moduleData = $this->objectManager->get(ModuleData::class);
60
        } else {
61
            $moduleData = unserialize($moduleData);
62
        }
63
64
        return $moduleData;
65
    }
66
67
    /**
68
     * Persists serialized module data to user settings
69
     *
70
     * @param ModuleData $moduleData
71
     * @return void
72
     */
73
    public function persistModuleData(ModuleData $moduleData)
74
    {
75
        $GLOBALS['BE_USER']->pushModuleData(self::KEY, serialize($moduleData));
76
    }
77
78
    /**
79
     * Unsets not serializable module data.
80
     *
81
     * @param string|null $serializedModuleData
82
     */
83
    private function unsetModuleDataIfCanNotBeSerialized(string &$serializedModuleData = null)
84
    {
85
        if (!isset($serializedModuleData)) {
86
            $serializedModuleData = '';
87
            return;
88
        }
89
        if (false !== strpos($serializedModuleData, 'ApacheSolrForTypo3\\Solr\\Domain\\Model\\ModuleData')
90
            || false !== strpos($serializedModuleData, 'Tx_Solr_Site')) {
91
            $serializedModuleData = '';
92
        }
93
    }
94
}
95