<commit_before><?php

/**
 * This file is part of the Tarantool Client package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace Tarantool\Client\Middleware;

use Tarantool\Client\Handler\Handler;
use Tarantool\Client\Request\AuthenticateRequest;
use Tarantool\Client\Request\Request;
use Tarantool\Client\Response;

final class AuthenticationMiddleware implements Middleware
{
    private $username;
    private $password;
    private $greeting;

    public function __construct(string $username, string $password = '')
    {
        $this->username = $username;
        $this->password = $password;
    }

    public function process(Request $request, Handler $handler) : Response
    {
        $connection = $handler->getConnection();

        if ($this->greeting !== $greeting = $connection->open()) {
            $handler->handle(new AuthenticateRequest(
                $greeting->getSalt(),
                $this->username,
                $this->password
            ));

            $this->greeting = $greeting;
        }

        return $handler->handle($request);
    }
}<commit_msg>Fix calling getSalt() on possibly null value in AuthenticationMiddleware<commit_after><?php

/**
 * This file is part of the Tarantool Client package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace Tarantool\Client\Middleware;

use Tarantool\Client\Handler\Handler;
use Tarantool\Client\Request\AuthenticateRequest;
use Tarantool\Client\Request\Request;
use Tarantool\Client\Response;

final class AuthenticationMiddleware implements Middleware
{
    private $username;
    private $password;
    private $greeting;

    public function __construct(string $username, string $password = '')
    {
        $this->username = $username;
        $this->password = $password;
    }

    public function process(Request $request, Handler $handler) : Response
    {
        $greeting = $handler->getConnection()->open();

        if ($greeting && $greeting !== $this->greeting) {
            $handler->handle(new AuthenticateRequest(
                $greeting->getSalt(),
                $this->username,
                $this->password
            ));

            $this->greeting = $greeting;
        }

        return $handler->handle($request);
    }
}
{'fraction_non_alphanumeric': 0.11605035405192761, 'fraction_numerical': 0.0007867820613690008, 'mean_word_length': 2.8706240487062407, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1073996', 'n_tokens_mistral': 698, 'n_tokens_neox': 694, 'n_words': 180}