ModuleDataStorageService   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
eloc 16
c 0
b 0
f 0
dl 0
loc 52
ccs 0
cts 24
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A persistModuleData() 0 3 1
A unsetModuleDataIfCanNotBeSerialized() 0 9 4
A loadModuleData() 0 12 3
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 3 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
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * Module data storage service. Used to store and retrieve module state (eg.
33
 * checkboxes, selections).
34
 */
35
class ModuleDataStorageService implements SingletonInterface
36
{
37
38
    /**
39
     * @var string
40
     */
41
    const KEY = 'tx_solr';
42
43
    /**
44
     * Loads module data for user settings or returns a fresh object initially
45
     *
46
     * @return ModuleData
47
     */
48
    public function loadModuleData()
49
    {
50
        $moduleData = $GLOBALS['BE_USER']->getModuleData(self::KEY);
51
52
        $this->unsetModuleDataIfCanNotBeSerialized($moduleData);
53
        if (empty($moduleData) || !$moduleData) {
54
            $moduleData = GeneralUtility::makeInstance(ModuleData::class);
55
        } else {
56
            $moduleData = unserialize($moduleData);
57
        }
58
59
        return $moduleData;
60
    }
61
62
    /**
63
     * Persists serialized module data to user settings
64
     *
65
     * @param ModuleData $moduleData
66
     * @return void
67
     */
68
    public function persistModuleData(ModuleData $moduleData)
69
    {
70
        $GLOBALS['BE_USER']->pushModuleData(self::KEY, serialize($moduleData));
71
    }
72
73
    /**
74
     * Unsets not serializable module data.
75
     *
76
     * @param string|null $serializedModuleData
77
     */
78
    private function unsetModuleDataIfCanNotBeSerialized(string &$serializedModuleData = null)
79
    {
80
        if (!isset($serializedModuleData)) {
81
            $serializedModuleData = '';
82
            return;
83
        }
84
        if (false !== strpos($serializedModuleData, 'ApacheSolrForTypo3\\Solr\\Domain\\Model\\ModuleData')
85
            || false !== strpos($serializedModuleData, 'Tx_Solr_Site')) {
86
            $serializedModuleData = '';
87
        }
88
    }
89
}
90