Passed
Pull Request — master (#1317)
by
unknown
21:24
created

ModuleDataStorageService::loadModuleData()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 0
crap 12
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