dkd-kaehm /
ext-solr
| 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'])) { |
|
| 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
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 | 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 | * 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 | public function getRequestId() |
||
| 144 | { |
||
| 145 | 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 | public function setRequestId($requestId) |
||
| 155 | { |
||
| 156 | $this->requestId = $requestId; |
||
| 157 | } |
||
| 158 | } |
||
| 159 |