Basho\Riak\Api\Http::parseResponse PHP Method

parseResponse() protected method

protected parseResponse ( )
    protected function parseResponse()
    {
        // trim leading / trailing whitespace
        $body = $this->responseBody;
        $location = null;
        if ($this->getResponseHeader('Location')) {
            $location = Location::fromString($this->getResponseHeader('Location'));
        }
        if ($this->statusCode == 500) {
            $this->success = false;
            $this->error = $body;
        }
        switch (get_class($this->command)) {
            case 'Basho\\Riak\\Command\\Bucket\\Store':
            case 'Basho\\Riak\\Command\\Bucket\\Fetch':
                $bucket = null;
                $modified = $this->getResponseHeader(static::LAST_MODIFIED_KEY, '');
                $properties = json_decode($body, true);
                if (isset($properties['props']) && $this->command->getBucket()) {
                    $bucket = new Bucket($this->command->getBucket()->getName(), $this->command->getBucket()->getType(), $properties['props']);
                }
                $response = new Command\Bucket\Response($this->success, $this->statusCode, $this->error, $bucket, $modified);
                break;
            case 'Basho\\Riak\\Command\\Object\\Fetch':
            case 'Basho\\Riak\\Command\\Object\\Store':
                /** @var Command\Object $command */
                $command = $this->command;
                $objects = (new Api\Http\Translator\ObjectResponse($command, $this->statusCode))->parseResponse($body, $this->responseHeaders);
                $response = new Command\Object\Response($this->success, $this->statusCode, $this->error, $location, $objects);
                break;
            case 'Basho\\Riak\\Command\\Object\\FetchPreflist':
                $response = new Command\Object\Response($this->success, $this->statusCode, $this->error, $location, [new Object(json_decode($body))]);
                break;
            case 'Basho\\Riak\\Command\\DataType\\Counter\\Store':
            case 'Basho\\Riak\\Command\\DataType\\Counter\\Fetch':
                $counter = null;
                $json_object = json_decode($body);
                if ($json_object && isset($json_object->value)) {
                    $counter = new Counter($json_object->value);
                }
                $response = new Command\DataType\Counter\Response($this->success, $this->statusCode, $this->error, $location, $counter, $this->getResponseHeader('Date'));
                break;
            case 'Basho\\Riak\\Command\\DataType\\Set\\Store':
            case 'Basho\\Riak\\Command\\DataType\\Set\\Fetch':
                $set = null;
                $json_object = json_decode($body);
                if ($json_object && isset($json_object->value)) {
                    $set = new Set($json_object->value, $json_object->context);
                }
                $response = new Command\DataType\Set\Response($this->success, $this->statusCode, $this->error, $location, $set, $this->getResponseHeader('Date'));
                break;
            case 'Basho\\Riak\\Command\\DataType\\Map\\Store':
            case 'Basho\\Riak\\Command\\DataType\\Map\\Fetch':
                $map = null;
                $json_object = json_decode($body, true);
                if ($json_object && isset($json_object['value'])) {
                    $map = new Map($json_object['value'], $json_object['context']);
                }
                $response = new Command\DataType\Map\Response($this->success, $this->statusCode, $this->error, $location, $map, $this->getResponseHeader('Date'));
                break;
            case 'Basho\\Riak\\Command\\DataType\\Hll\\Store':
            case 'Basho\\Riak\\Command\\DataType\\Hll\\Fetch':
                $hll = null;
                $json_object = json_decode($body);
                if ($json_object && isset($json_object->value)) {
                    $hll = new Hll($json_object->value);
                }
                $response = new Command\DataType\Hll\Response($this->success, $this->statusCode, $this->error, $location, $hll, $this->getResponseHeader('Date'));
                break;
            case 'Basho\\Riak\\Command\\Search\\Fetch':
                $results = in_array($this->statusCode, [200, 204]) ? json_decode($body) : null;
                $docs = [];
                if (!empty($results->response->docs)) {
                    foreach ($results->response->docs as $doc) {
                        $docs[] = new Doc($doc);
                    }
                }
                $numFound = !empty($results->response->numFound) ? $results->response->numFound : 0;
                $response = new Command\Search\Response($this->success, $this->statusCode, $this->error, $numFound, $docs);
                break;
            case 'Basho\\Riak\\Command\\Search\\Index\\Store':
            case 'Basho\\Riak\\Command\\Search\\Index\\Fetch':
                $index = json_decode($body);
                $response = new Command\Search\Index\Response($this->success, $this->statusCode, $this->error, $index);
                break;
            case 'Basho\\Riak\\Command\\Search\\Schema\\Store':
            case 'Basho\\Riak\\Command\\Search\\Schema\\Fetch':
                $response = new Command\Search\Schema\Response($this->success, $this->statusCode, $this->error, $body, $this->getResponseHeader(static::CONTENT_TYPE_KEY));
                break;
            case 'Basho\\Riak\\Command\\MapReduce\\Fetch':
                $results = in_array($this->statusCode, [200, 204]) ? json_decode($body) : null;
                $response = new Command\MapReduce\Response($this->success, $this->statusCode, $this->error, $results);
                break;
            case 'Basho\\Riak\\Command\\Indexes\\Query':
                $json_object = in_array($this->statusCode, [200, 204]) ? json_decode($body, true) : null;
                $results = [];
                $termsReturned = false;
                $continuation = null;
                $done = true;
                if (isset($json_object['keys'])) {
                    $results = $json_object['keys'];
                }
                if (isset($json_object['results'])) {
                    $results = $json_object['results'];
                    $termsReturned = true;
                }
                if (isset($json_object['continuation'])) {
                    $continuation = $json_object['continuation'];
                    $done = false;
                }
                $response = new Command\Indexes\Response($this->success, $this->statusCode, $this->error, $results, $termsReturned, $continuation, $done, $this->getResponseHeader('Date'));
                break;
            case 'Basho\\Riak\\Command\\Stats':
                $response = new Command\Stats\Response($this->success, $this->statusCode, $this->error, json_decode($body, true));
                break;
            case 'Basho\\Riak\\Command\\TimeSeries\\Fetch':
                $row = in_array($this->statusCode, ['200', '201', '204']) ? json_decode($body, true) : [];
                $response = new Command\TimeSeries\Response($this->success, $this->statusCode, $this->error, [$row]);
                break;
            case 'Basho\\Riak\\Command\\TimeSeries\\Query\\Fetch':
                $results = in_array($this->statusCode, ['200', '204']) ? json_decode($body) : [];
                $rows = [];
                if (isset($results->rows)) {
                    foreach ($results->rows as $row) {
                        $cells = [];
                        foreach ($results->columns as $index => $column) {
                            $cells[$column] = $row[$index];
                        }
                        $rows[] = $cells;
                    }
                }
                $response = new Command\TimeSeries\Query\Response($this->success, $this->statusCode, $this->error, $rows);
                break;
            case 'Basho\\Riak\\Command\\TimeSeries\\Store':
            case 'Basho\\Riak\\Command\\TimeSeries\\Delete':
            case 'Basho\\Riak\\Command\\Object\\Delete':
            case 'Basho\\Riak\\Command\\Bucket\\Delete':
            case 'Basho\\Riak\\Command\\Search\\Index\\Delete':
            case 'Basho\\Riak\\Command\\Ping':
            default:
                $response = new Command\Response($this->success, $this->statusCode, $this->error);
                break;
        }
        $this->response = $response;
    }