<commit_before><?php

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

namespace Snide\Travis;

use Snide\Travis\Hydrator\SimpleHydrator;
use Snide\Travis\Model\Build;
use Snide\Travis\Model\Repository;
use Guzzle\Http\Client as GuzzleClient;

/**
 * Class Client
 *
 * @author <NAME> <<EMAIL>>
 */
class Client
{
    /**
     * Magnum CI API host
     *
     * @var string
     */
    protected $endpoint = 'https://api.travis-ci.org';

    /**
     * Hydrate object from array  data
     *
     * @var SimpleHydrator
     */
    protected $hydrator;

    /**
     * @var Client
     */
    protected $client;

    /**
     * Constructor
     *
     * @param SimpleHydrator $hydrator
     */
    public function __construct(SimpleHydrator $hydrator = null)
    {
        if (!$hydrator) {
            $this->hydrator = new SimpleHydrator();
        } else {
            $this->hydrator = $hydrator;
        }

        $this->client = new GuzzleClient($this->endpoint, array());
    }

    /**
     * Fetch project into project instance
     *
     * @param Repository $repository
     * @param bool $withBuild (Indicate if builds should be retrieve
     * @return \Snide\Travis\Model\Repository
     */
    public function fetchRepository(Repository $repository, $withBuild = true)
    {
        $response = $this->getResponse(
            sprintf('/repos/%s',$repository->getSlug()),
            array()
        );
        // Hydrate response into repository
        $project = $this->hydrate($repository, $response);

        if (!$withBuild) {
            return $project;
        }
        // Load Repository builds
        return $this->fetchBuilds($repository);
    }

    /**
     * Fetch builds into proejct instance
     *
     * @param Repository $repository
     * @return \Snide\Travis\Model\Repository
     */
    public function fetchBuilds(Repository $repository)
    {
        $response = $this->getResponse(
            sprintf('/repos/%s/builds',$repository->getSlug()),
            array());

        if (is_array($response)) {
            // Load project builds
            foreach ($response as $buildData) {
                $build = new Build();
                // Add build to repository instance
                $repository->addBuild($this->hydrate($build, $buildData));
            }
        }

        return $repository;
    }

    /**
     * @param \Snide\Travis\Client $client
     */
    public function setClient($client)
    {
        $this->client = $client;
    }

    /**
     * @return \Snide\Travis\Client
     */
    public function getClient()
    {
        return $this->client;
    }

    /**
     * Get Response from API
     * Response is an array (Result of json_decode)
     *
     * @param string $uri API URI
     * @param array $queryParams Query params
     * @return mixed
     */
    protected function getResponse($uri, array $queryParams = array())
    {
        $request = $this->client->get($uri, array(), array('query' => $queryParams));

        return $request->send()->json();
    }

    /**
     * Hydrate object with data
     *
     * @param mixed $object An object
     * @param array $data data to inject
     * @return mixed
     */
    protected function hydrate($object, $data)
    {
        return $this->hydrator->hydrate($object, $data);
    }
}<commit_msg>Change client name & add subscriber method<commit_after><?php

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

namespace Snide\Travis;

use Snide\Travis\Hydrator\SimpleHydrator;
use Snide\Travis\Model\Build;
use Snide\Travis\Model\Repository;
use Guzzle\Http\Client as GuzzleClient;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Class Client
 *
 * @author <NAME> <<EMAIL>>
 */
class Client
{
    /**
     * Magnum CI API host
     *
     * @var string
     */
    protected $endpoint = 'https://api.travis-ci.org';

    /**
     * Hydrate object from array  data
     *
     * @var SimpleHydrator
     */
    protected $hydrator;

    /**
     * @var Client
     */
    protected $httpClient;

    /**
     * Constructor
     *
     * @param SimpleHydrator $hydrator
     */
    public function __construct(SimpleHydrator $hydrator = null)
    {
        if (!$hydrator) {
            $this->hydrator = new SimpleHydrator();
        } else {
            $this->hydrator = $hydrator;
        }

        $this->httpClient = new GuzzleClient($this->endpoint, array());
    }

    /**
     * Fetch project into project instance
     *
     * @param Repository $repository
     * @param bool $withBuild (Indicate if builds should be retrieve
     * @return \Snide\Travis\Model\Repository
     */
    public function fetchRepository(Repository $repository, $withBuild = true)
    {
        $response = $this->getResponse(
            sprintf('/repos/%s',$repository->getSlug()),
            array()
        );
        // Hydrate response into repository
        $project = $this->hydrate($repository, $response);

        if (!$withBuild) {
            return $project;
        }
        // Load Repository builds
        return $this->fetchBuilds($repository);
    }

    /**
     * Fetch builds into proejct instance
     *
     * @param Repository $repository
     * @return \Snide\Travis\Model\Repository
     */
    public function fetchBuilds(Repository $repository)
    {
        $response = $this->getResponse(
            sprintf('/repos/%s/builds',$repository->getSlug()),
            array());

        if (is_array($response)) {
            // Load project builds
            foreach ($response as $buildData) {
                $build = new Build();
                // Add build to repository instance
                $repository->addBuild($this->hydrate($build, $buildData));
            }
        }

        return $repository;
    }

    /**
     * Add Http client subscriber
     *
     * @param EventSubscriberInterface $subscriber
     */
    public function addSubscriber(EventSubscriberInterface $subscriber)
    {
        $this->httpClient->addSubscriber($subscriber);
    }

    /**
     * Get Response from API
     * Response is an array (Result of json_decode)
     *
     * @param string $uri API URI
     * @param array $queryParams Query params
     * @return mixed
     */
    protected function getResponse($uri, array $queryParams = array())
    {
        $request = $this->httpClient->get($uri, array(), array('query' => $queryParams));

        return $request->send()->json();
    }

    /**
     * Hydrate object with data
     *
     * @param mixed $object An object
     * @param array $data data to inject
     * @return mixed
     */
    protected function hydrate($object, $data)
    {
        return $this->hydrator->hydrate($object, $data);
    }
}
{'fraction_non_alphanumeric': 0.12549407114624506, 'fraction_numerical': 0.0, 'mean_word_length': 2.077758470894874, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 57, 'https://': 2, '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': '93426', 'n_tokens_mistral': 2064, 'n_tokens_neox': 2016, 'n_words': 510}