Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
54.55% covered (warning)
54.55%
6 / 11
CRAP
71.43% covered (warning)
71.43%
25 / 35
ReindexRequest
0.00% covered (danger)
0.00%
0 / 1
54.55% covered (warning)
54.55%
6 / 11
20.25
71.43% covered (warning)
71.43%
25 / 35
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 setRemoteInfo
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setScript
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 setSize
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setRequestsPerSecond
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setSlices
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 reindexTask
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 reindex
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 request
0.00% covered (danger)
0.00%
0 / 1
3.47
62.50% covered (warning)
62.50%
5 / 8
 toArray
0.00% covered (danger)
0.00%
0 / 1
3.03
85.71% covered (warning)
85.71%
6 / 7
 asSourceDest
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
namespace CirrusSearch\Elastica;
use Elastica\Client;
use Elastica\Index;
use Elastica\Request;
use Elastica\Response;
use InvalidArgumentException;
class ReindexRequest {
    /** @var array */
    private $source;
    /** @var array */
    private $dest;
    /** @var array|null */
    private $script;
    /** @var int|null */
    private $size;
    /** @var int */
    private $requestsPerSecond = -1;
    /** @var int */
    private $slices = 1;
    /** @var Client */
    private $client;
    /**
     * @param Index $source
     * @param Index $dest
     * @param int $chunkSize
     */
    public function __construct( Index $source, Index $dest, $chunkSize = 100 ) {
        $this->source = $this->asSourceDest( $source );
        $this->source['size'] = $chunkSize;
        $this->dest = $this->asSourceDest( $dest );
        $this->client = $dest->getClient();
    }
    /**
     * @param array $remote
     * @return $this
     */
    public function setRemoteInfo( array $remote ) {
        $this->source['remote'] = $remote;
        return $this;
    }
    /**
     * @param array $script
     * @return $this
     */
    public function setScript( array $script ) {
        $this->script = $script;
        return $this;
    }
    /**
     * The number of documents to reindex
     *
     * @param int $size
     * @return $this
     */
    public function setSize( $size ) {
        $this->size = $size;
        return $this;
    }
    /**
     * @param int $rps
     * @return $this
     */
    public function setRequestsPerSecond( $rps ) {
        $this->requestsPerSecond = $rps;
        return $this;
    }
    /**
     * @param int $slices
     * @return $this
     */
    public function setSlices( $slices ) {
        $this->slices = $slices;
        return $this;
    }
    /**
     * @return ReindexTask
     */
    public function reindexTask() {
        $response = $this->request( [
            'wait_for_completion' => 'false',
        ] );
        return new ReindexTask( $this->client, $response->getData()['task'] );
    }
    /**
     * @return ReindexResponse
     */
    public function reindex() {
        return new ReindexResponse( $this->request()->getData() );
    }
    /**
     * @param array $query
     * @return Response
     */
    private function request( array $query = [] ) {
        $query['requests_per_second'] = $this->requestsPerSecond;
        $query['slices'] = $this->slices;
        $response = $this->client->request( '_reindex', Request::POST, $this->toArray(), $query );
        if ( !$response->isOK() ) {
            throw new \Exception( $response->hasError()
                ? 'Failed reindex request: ' . $response->getErrorMessage()
                : 'Unknown reindex failure: ' . $response->getStatus()
            );
        }
        return $response;
    }
    /**
     * @return array
     */
    public function toArray() {
        $request = [
            'source' => $this->source,
            'dest' => $this->dest,
        ];
        if ( $this->script ) {
            $request['script'] = $this->script;
        }
        if ( $this->size ) {
            $request['size'] = $this->size;
        }
        return $request;
    }
    /**
     * @param Index $input
     * @return array
     * @throws InvalidArgumentException
     */
    private function asSourceDest( Index $input ) {
        return [ 'index' => $input->getName() ];
    }
}