Passed
Push — task/2976_TYPO3.11_compatibili... ( 4d1a77...3b9190 )
by Rafael
03:39
created

PageIndexerResponse::getResultsFromJson()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4
1
<?php
2
namespace ApacheSolrForTypo3\Solr\IndexQueue;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-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
/**
28
 * Index Queue Page Indexer response to provide data for requested actions.
29
 *
30
 * @author Ingo Renner <[email protected]>
31
 */
32
class PageIndexerResponse
33
{
34
35
    /**
36
     * Unique request ID.
37
     *
38
     * @var string
39
     */
40
    protected $requestId = null;
41
42
    /**
43
     * The actions' results as action => result pairs.
44
     *
45
     * @var array
46
     */
47
    protected $results = [];
48
49
    /**
50
     * Turns a JSON encoded result string back into its PHP representation.
51
     *
52
     * @param string $jsonEncodedResponse JSON encoded result string
53
     * @return array|bool An array of action => result pairs or FALSE if the response could not be decoded
54
     */
55 5
    public static function getResultsFromJson($jsonEncodedResponse)
56
    {
57 5
        $responseData = json_decode($jsonEncodedResponse, true);
58
59 5
        if (is_array($responseData['actionResults'] ?? null)) {
60 4
            foreach ($responseData['actionResults'] as $action => $serializedActionResult) {
61 4
                $responseData['actionResults'][$action] = unserialize($serializedActionResult);
62
            }
63 1
        } elseif (is_null($responseData)) {
64 1
            $responseData = false;
65
        }
66
67 5
        return $responseData;
68
    }
69
70
    /**
71
     * Adds an action's result.
72
     *
73
     * @param string $action The action name.
74
     * @param mixed $result The action's result.
75
     * @throws \RuntimeException if $action is null
76
     */
77 5
    public function addActionResult($action, $result)
78
    {
79 5
        if (is_null($action)) {
0 ignored issues
show
introduced by
The condition is_null($action) is always false.
Loading history...
80
            throw new \RuntimeException(
81
                'Attempt to provide a result without providing an action',
82
                1294080509
83
            );
84
        }
85
86 5
        $this->results[$action] = $result;
87 5
    }
88
89
    /**
90
     * Gets the complete set of results or a specific action's results.
91
     *
92
     * @param string $action Optional action name.
93
     * @return array
94
     */
95 3
    public function getActionResult($action = null)
96
    {
97 3
        $result = $this->results;
98
99 3
        if (!empty($action)) {
100 2
            $result = $this->results[$action];
101
        }
102
103 3
        return $result;
104
    }
105
106
    /**
107
     * Compiles the response's content so that it can be sent back to the
108
     * Index Queue page indexer.
109
     *
110
     * @return string The response content
111
     */
112
    public function getContent()
113
    {
114
        return $this->toJson();
115
    }
116
117
    /**
118
     * Converts the response's data to JSON.
119
     *
120
     * @return string JSON representation of the results.
121
     */
122
    protected function toJson()
123
    {
124
        $serializedActionResults = [];
125
126
        foreach ($this->results as $action => $result) {
127
            $serializedActionResults[$action] = serialize($result);
128
        }
129
130
        $responseData = [
131
            'requestId' => $this->requestId,
132
            'actionResults' => $serializedActionResults
133
        ];
134
135
        return json_encode($responseData);
136
    }
137
138
    /**
139
     * Gets the Id of the request this response belongs to.
140
     *
141
     * @return string Request Id.
142
     */
143 1
    public function getRequestId()
144
    {
145 1
        return $this->requestId;
146
    }
147
148
    /**
149
     * Sets the Id of the request this response belongs to.
150
     *
151
     * @param string $requestId Request Id.
152
     * @return void
153
     */
154 3
    public function setRequestId($requestId)
155
    {
156 3
        $this->requestId = $requestId;
157 3
    }
158
}
159