<commit_before><?php

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

namespace Lug\Component\Behat\Extension\Api\Context;

use Behat\Gherkin\Node\PyStringNode;
use Coduo\PHPMatcher\Matcher;
use Http\Client\Exception\HttpException;
use Http\Client\HttpClient;
use Http\Message\RequestFactory;
use Lug\Component\Behat\Extension\Api\Matcher\MatcherFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * @author GeLo <<EMAIL>>
 */
class ApiContext implements ApiContextInterface
{
    /**
     * @var HttpClient
     */
    private $client;

    /**
     * @var RequestFactory
     */
    private $requestFactory;

    /**
     * @var string
     */
    private $baseUrl;

    /**
     * @var string[][]
     */
    private $headers = [];

    /**
     * @var RequestInterface|null
     */
    private $request;

    /**
     * @var ResponseInterface|null
     */
    private $response;

    /**
     * @var Matcher
     */
    private $matcher;

    public function __construct()
    {
        $this->matcher = (new MatcherFactory())->createMatcher();
    }

    /**
     * {@inheritdoc}
     */
    public function setClient(HttpClient $client)
    {
        $this->client = $client;
    }

    /**
     * {@inheritdoc}
     */
    public function setRequestFactory(RequestFactory $requestFactory)
    {
        $this->requestFactory = $requestFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function setBaseUrl($baseUrl)
    {
        if (strrpos($baseUrl, '/') === strlen($baseUrl) - 1) {
            $baseUrl = substr($baseUrl, 0, -1);
        }

        $this->baseUrl = $baseUrl;
    }

    /**
     * @param string $header
     * @param string $value
     *
     * @Given I set the header ":header" with value ":value"
     */
    public function setHeader($header, $value)
    {
        $this->headers[$header][] = $value;
    }

    /**
     * @param string $method
     * @param string $url
     *
     * @When I send a ":method" request to ":url"
     */
    public function send($method, $url)
    {
        $this->request = $this->requestFactory->createRequest($method, $this->prepareUrl($url), $this->headers);

        $this->sendRequest();
    }

    /**
     * @param string       $method<commit_msg>[Behat] Fix API context header<commit_after><?php

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

namespace Lug\Component\Behat\Extension\Api\Context;

use Behat\Gherkin\Node\PyStringNode;
use Coduo\PHPMatcher\Matcher;
use Http\Client\Exception\HttpException;
use Http\Client\HttpClient;
use Http\Message\RequestFactory;
use Lug\Component\Behat\Extension\Api\Matcher\MatcherFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * @author GeLo <<EMAIL>>
 */
class ApiContext implements ApiContextInterface
{
    /**
     * @var HttpClient
     */
    private $client;

    /**
     * @var RequestFactory
     */
    private $requestFactory;

    /**
     * @var string
     */
    private $baseUrl;

    /**
     * @var string[][]
     */
    private $headers = [];

    /**
     * @var RequestInterface|null
     */
    private $request;

    /**
     * @var ResponseInterface|null
     */
    private $response;

    /**
     * @var Matcher
     */
    private $matcher;

    public function __construct()
    {
        $this->matcher = (new MatcherFactory())->createMatcher();
    }

    /**
     * {@inheritdoc}
     */
    public function setClient(HttpClient $client)
    {
        $this->client = $client;
    }

    /**
     * {@inheritdoc}
     */
    public function setRequestFactory(RequestFactory $requestFactory)
    {
        $this->requestFactory = $requestFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function setBaseUrl($baseUrl)
    {
        if (strrpos($baseUrl, '/') === strlen($baseUrl) - 1) {
            $baseUrl = substr($baseUrl, 0, -1);
        }

        $this->baseUrl = $baseUrl;
    }

    /**
     * @param string $header
     * @param string $value
     *
     * @Given I set the header ":header" with value ":value"
     */
    public function setHeader($header, $value)
    {
        if (!isset($this->headers[$header]) || !in_array($value, $this->headers[$header], true)) {
            $this->headers[$header][] = $value;
        }
    }

    /**
     * @param string $method
     * @param string $url
     *
     * @When I send a ":method" request to ":url"
     */
    public function send($method, $url)
    {
        $this->request = $this->requestFactory->createRequest($method, $this->prepareUrl($url), $this->headers);

        $this->sendRequest();
    }

    /**
     * @param string       $method
{'fraction_non_alphanumeric': 0.14694122351059577, 'fraction_numerical': 0.0011995201919232307, 'mean_word_length': 2.2763588736083826, 'pattern_counts': {'":': 8, '<': 15, '<?xml version=': 0, '>': 39, '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': '933349', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1509, 'n_words': 338}