Completed
Branch master (b48c7c)
by Timo
11:16 queued 17s
created

PageIndexerResponse::getResultsFromJson()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
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 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
/**
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 = array();
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'])) {
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)) {
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 4
    public function getActionResult($action = null)
96
    {
97 4
        $result = $this->results;
98
99 4
        if (!empty($action)) {
100 3
            $result = $this->results[$action];
101
        }
102
103 4
        return $result;
104
    }
105
106
    /**
107
     * Sends the response's headers.
108
     *
109
     * @return void
110
     */
111
    public function sendHeaders()
112
    {
113
        // This overwrites the "Content-Encoding: gzip" header that is usually sent by TYPO3 by default. This header
114
        // would require that the content really is gzip-ed (which it is not). This lets e.g. Varnish 3.0
115
        // fail when trying to decode the response.
116
        header('Content-Encoding: none');
117
118
        header('Content-Length: ' . strlen($this->getContent()));
119
    }
120
121
    /**
122
     * Compiles the response's content so that it can be sent back to the
123
     * Index Queue page indexer.
124
     *
125
     * @return string The response content
126
     */
127
    public function getContent()
128
    {
129
        return $this->toJson();
130
    }
131
132
    /**
133
     * Converts the response's data to JSON.
134
     *
135
     * @return string JSON representation of the results.
136
     */
137
    protected function toJson()
138
    {
139
        $serializedActionResults = array();
140
141
        foreach ($this->results as $action => $result) {
142
            $serializedActionResults[$action] = serialize($result);
143
        }
144
145
        $responseData = array(
146
            'requestId' => $this->requestId,
147
            'actionResults' => $serializedActionResults
148
        );
149
150
        return json_encode($responseData);
151
    }
152
153
    /**
154
     * Gets the Id of the request this response belongs to.
155
     *
156
     * @return string Request Id.
157
     */
158 1
    public function getRequestId()
159
    {
160 1
        return $this->requestId;
161
    }
162
163
    /**
164
     * Sets the Id of the request this response belongs to.
165
     *
166
     * @param string $requestId Request Id.
167
     * @return void
168
     */
169 3
    public function setRequestId($requestId)
170
    {
171 3
        $this->requestId = $requestId;
172 3
    }
173
}
174