vendor/ruflin/elastica/src/Connection/ConnectionPool.php line 16

Open in your IDE?
  1. <?php
  2. namespace Elastica\Connection;
  3. use Elastica\Client;
  4. use Elastica\Connection;
  5. use Elastica\Connection\Strategy\StrategyInterface;
  6. use Elastica\Exception\ClientException;
  7. use Exception;
  8. /**
  9.  * Description of ConnectionPool.
  10.  *
  11.  * @author chabior
  12.  */
  13. class ConnectionPool
  14. {
  15.     /**
  16.      * @var array|Connection[] Connections array
  17.      */
  18.     protected $_connections;
  19.     /**
  20.      * @var StrategyInterface Strategy for connection
  21.      */
  22.     protected $_strategy;
  23.     /**
  24.      * @var callable|null Function called on connection fail
  25.      */
  26.     protected $_callback;
  27.     public function __construct(array $connectionsStrategyInterface $strategy, ?callable $callback null)
  28.     {
  29.         $this->_connections $connections;
  30.         $this->_strategy $strategy;
  31.         $this->_callback $callback;
  32.     }
  33.     /**
  34.      * @return $this
  35.      */
  36.     public function addConnection(Connection $connection): self
  37.     {
  38.         $this->_connections[] = $connection;
  39.         return $this;
  40.     }
  41.     /**
  42.      * @param Connection[] $connections
  43.      *
  44.      * @return $this
  45.      */
  46.     public function setConnections(array $connections): self
  47.     {
  48.         $this->_connections $connections;
  49.         return $this;
  50.     }
  51.     public function hasConnection(): bool
  52.     {
  53.         foreach ($this->_connections as $connection) {
  54.             if ($connection->isEnabled()) {
  55.                 return true;
  56.             }
  57.         }
  58.         return false;
  59.     }
  60.     /**
  61.      * @return Connection[]
  62.      */
  63.     public function getConnections(): array
  64.     {
  65.         return $this->_connections;
  66.     }
  67.     /**
  68.      * @throws ClientException
  69.      */
  70.     public function getConnection(): Connection
  71.     {
  72.         return $this->_strategy->getConnection($this->getConnections());
  73.     }
  74.     public function onFail(Connection $connectionException $eClient $client): void
  75.     {
  76.         $connection->setEnabled(false);
  77.         if ($this->_callback) {
  78.             ($this->_callback)($connection$e$client);
  79.         }
  80.     }
  81.     public function getStrategy(): StrategyInterface
  82.     {
  83.         return $this->_strategy;
  84.     }
  85. }