<commit_before><?php
namespace SimplyTestable\WorkerBundle\Services;

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Cookie\SetCookie;
use GuzzleHttp\Message\RequestInterface;
use GuzzleHttp\Subscriber\Cache\CacheSubscriber;
use GuzzleHttp\Subscriber\Cache\CacheStorage;
use GuzzleHttp\Subscriber\Cookie as HttpCookieSubscriber;
use GuzzleHttp\Subscriber\Retry\RetrySubscriber as HttpRetrySubscriber;
use GuzzleHttp\Subscriber\History as HttpHistorySubscriber;

class HttpClientService
{
    /**
     * @var HttpClient
     */
    protected $httpClient = null;

    /**
     * @var HttpCache
     */
    private $httpCache = null;

    /**
     * @var array
     */
    private $curlOptions = array();

    /**
     * @var HttpHistorySubscriber
     */
    private $historySubscriber;

    /**
     * @var HttpCookieSubscriber
     */
    private $cookieSubscriber;

    /**
     * @var HttpRetrySubscriber
     */
    private $retrySubscriber;

    /**
     * @param HttpCache $httpCache
     * @param array $curlOptions
     */
    public function __construct(
        HttpCache $httpCache,
        $curlOptions
    ) {
        $this->httpCache = $httpCache;

        foreach ($curlOptions as $curlOption) {
            if (defined($curlOption['name'])) {
                $this->curlOptions[constant($curlOption['name'])] = $curlOption['value'];
            }
        }

        $this->historySubscriber = new HttpHistorySubscriber();
        $this->cookieSubscriber = new HttpCookieSubscriber();
        $this->retrySubscriber = $this->createRetrySubscriber();

        $this->httpClient = new HttpClient([
            'config' => [
                'curl' => $this->curlOptions
            ],
        ]);

        $cacheSubscriber = $this->createCacheSubscriber();
        if (!is_null($cacheSubscriber)) {
            $this->httpClient->getEmitter()->attach($this->createCacheSubscriber());
        }

        $this->enableRetrySubscriber();
        $this->httpClient->getEmitter()->attach($this->historySubscriber);
        $this->httpClient->getEmitter()->attach($this->cookieSubscriber);
    }

    public function enableRetrySubscriber()
    {
        $this->httpClient->getEmitter()->attach($this->retrySubscriber);
    }

    public function disableRetrySubscriber()
    {
        $this->httpClient->getEmitter()->detach($this->retrySubscriber);
    }

    /**<commit_msg>Don't verify SSL connections (#59)<commit_after><?php
namespace SimplyTestable\WorkerBundle\Services;

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Cookie\SetCookie;
use GuzzleHttp\Message\RequestInterface;
use GuzzleHttp\Subscriber\Cache\CacheSubscriber;
use GuzzleHttp\Subscriber\Cache\CacheStorage;
use GuzzleHttp\Subscriber\Cookie as HttpCookieSubscriber;
use GuzzleHttp\Subscriber\Retry\RetrySubscriber as HttpRetrySubscriber;
use GuzzleHttp\Subscriber\History as HttpHistorySubscriber;

class HttpClientService
{
    /**
     * @var HttpClient
     */
    protected $httpClient = null;

    /**
     * @var HttpCache
     */
    private $httpCache = null;

    /**
     * @var array
     */
    private $curlOptions = array();

    /**
     * @var HttpHistorySubscriber
     */
    private $historySubscriber;

    /**
     * @var HttpCookieSubscriber
     */
    private $cookieSubscriber;

    /**
     * @var HttpRetrySubscriber
     */
    private $retrySubscriber;

    /**
     * @param HttpCache $httpCache
     * @param array $curlOptions
     */
    public function __construct(
        HttpCache $httpCache,
        $curlOptions
    ) {
        $this->httpCache = $httpCache;

        foreach ($curlOptions as $curlOption) {
            if (defined($curlOption['name'])) {
                $this->curlOptions[constant($curlOption['name'])] = $curlOption['value'];
            }
        }

        $this->historySubscriber = new HttpHistorySubscriber();
        $this->cookieSubscriber = new HttpCookieSubscriber();
        $this->retrySubscriber = $this->createRetrySubscriber();

        $this->httpClient = new HttpClient([
            'config' => [
                'curl' => $this->curlOptions
            ],
            'defaults' => [
                'verify' => false,
            ],
        ]);

        $cacheSubscriber = $this->createCacheSubscriber();
        if (!is_null($cacheSubscriber)) {
            $this->httpClient->getEmitter()->attach($this->createCacheSubscriber());
        }

        $this->enableRetrySubscriber();
        $this->httpClient->getEmitter()->attach($this->historySubscriber);
        $this->httpClient->getEmitter()->attach($this->cookieSubscriber);
    }

    public function enableRetrySubscriber()
    {
        $this->httpClient->getEmitter()->attach($this->retrySubscriber);
    }

    public function disableRetrySubscriber()
    {
        $this->httpClient->getEmitter()->detach($this->retrySubscriber);
    }

    /**
{'fraction_non_alphanumeric': 0.1309255079006772, 'fraction_numerical': 0.0004104247896572953, 'mean_word_length': 2.6427503736920777, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, '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': '2651904', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1359, 'n_words': 223}