evseevnn\Cassandra\Database::connect PHP Method

connect() public method

Connect to database
public connect ( ) : boolean
return boolean
    public function connect()
    {
        if ($this->connection->isConnected()) {
            return true;
        }
        $this->connection->connect();
        $response = $this->connection->sendRequest(RequestFactory::startup($this->options));
        $responseType = $response->getType();
        switch ($responseType) {
            case OpcodeEnum::ERROR:
                throw new ConnectionException($response->getData());
            case OpcodeEnum::AUTHENTICATE:
                $nodeOptions = $this->connection->getNode()->getOptions();
                $response = $this->connection->sendRequest(RequestFactory::credentials($nodeOptions['username'], $nodeOptions['password']));
                $responseType = $response->getType();
        }
        if ($responseType === OpcodeEnum::ERROR) {
            throw new ConnectionException($response->getData());
        }
        if (!empty($this->keyspace)) {
            $this->setKeyspace($this->keyspace);
        }
        return true;
    }

Usage Example

 public function testTimeoutConnection()
 {
     if (PHP_OS == 'Linux') {
         shell_exec('sudo iptables -A OUTPUT -p tcp -m tcp --sport 9042 -j DROP');
     } else {
         $this->markTestSkipped("Your OS " . PHP_OS . " can not run timeout tests, since they depend on iptables");
     }
     $start = time();
     try {
         $connection = new Cassandra\Database(['127.0.0.1:9042'], null, ['connect_timeout_ms' => 2500]);
         $connection->connect();
     } catch (Cassandra\Exception\ClusterException $e) {
         $catched = true;
     }
     $this->assertEquals(true, $catched);
     $timespan = time() - $start;
     shell_exec('sudo iptables -D OUTPUT -p tcp -m tcp --sport 9042 -j DROP');
     $this->assertLessThanOrEqual(3, $timespan);
     $this->assertGreaterThanOrEqual(2, $timespan);
 }
All Usage Examples Of evseevnn\Cassandra\Database::connect