<commit_before><?php

namespace App\Contributors\Service;

use App\Contributors\Model\Contributor;
use Github\Client;
use Github\ResultPager;
use Predis\Client as Redis;

/**
 * Class Repository.
 */
class Repository implements RepositoryInterface
{
    private const REDIS_KEY_CONTRIBUTORS = 'CONTRIBUTORS';

    protected $redis;
    protected $client;
    protected $resultPager;

    public function __construct(Redis $redis, Client $client, ResultPager $resultPager)
    {
        $this->redis = $redis;
        $this->client = $client;
        $this->resultPager = $resultPager;
    }

    /**
     * @return Contributor[]
     */
    public function all(): array
    {
        try {
            $contributorsByCache = $this->getContributorsByCache();

            if (null !== $contributorsByCache) {
                return unserialize($contributorsByCache, ['allowed_classes' => true]);
            }

            return $this->getContributors();
        } catch (\Exception $e) {
            return [];
        }
    }

    /**
     * @return int
     */
    public function updateCache(): int
    {
        $contributors = $this->getContributors();

        return \count($contributors);
    }

    /**
     * @return null|string
     */
    private function getContributorsByCache(): ?string
    {
        return $this->redis->get(self::REDIS_KEY_CONTRIBUTORS);
    }

    /**
     * @param Contributor[] $contributors
     */
    private function setContributorsInCache($contributors): void
    {
        $this->redis->set(self::REDIS_KEY_CONTRIBUTORS, serialize($contributors));
    }

    /**
     * @param bool $setCache
     *
     * @return Contributor[]
     */
    private function getContributors($setCache = true): array
    {
        $contributors = [];

        $results = $this->getContributorsByGithub('PUGX', 'badge-poser');
        foreach ($results as $result) {
            $contributors[$result['login']] = Contributor::create($result['login'], $result['html_url'], $result['avatar_url']);
        }
        $results = $this->getContributorsByGithub('badges', 'poser');
        foreach ($results as $result) {
            $contributors[$result['login']] = Contributor::create($result['login'], $result['html_url'], $result['avatar_url']);
        }

        if ($setCache) {
            $this->setContributorsInCache($contributors);
        }

        return $contributors;
    }

    /**
     * @param $username
     * @param $repoName
     *
     * @return array
     */
    private function getContributorsByGithub($username, $repoName): array
    {
        try {
            $repoApi = $this->client->api('repo');
            $parameters = [$username, $repoName];
            $results = $this->resultPager->fetchAll(
                $repoApi,
                'contributors',
                $parameters
            );
        } catch (\Exception $e) {
            return [];
        }

        return $results;
    }
}
<commit_msg>Remove catch on call to GitHub api. Bearer of unexpected situations.<commit_after><?php

namespace App\Contributors\Service;

use App\Contributors\Model\Contributor;
use Github\Client;
use Github\ResultPager;
use Predis\Client as Redis;

/**
 * Class Repository.
 */
class Repository implements RepositoryInterface
{
    private const REDIS_KEY_CONTRIBUTORS = 'CONTRIBUTORS';

    protected $redis;
    protected $client;
    protected $resultPager;

    public function __construct(Redis $redis, Client $client, ResultPager $resultPager)
    {
        $this->redis = $redis;
        $this->client = $client;
        $this->resultPager = $resultPager;
    }

    /**
     * @return Contributor[]
     */
    public function all(): array
    {
        try {
            $contributorsByCache = $this->getContributorsByCache();

            if (null !== $contributorsByCache) {
                return unserialize($contributorsByCache, ['allowed_classes' => true]);
            }

            return $this->getContributors();
        } catch (\Exception $e) {
            return [];
        }
    }

    /**
     * @return int
     */
    public function updateCache(): int
    {
        $contributors = $this->getContributors();

        return \count($contributors);
    }

    /**
     * @return null|string
     */
    private function getContributorsByCache(): ?string
    {
        return $this->redis->get(self::REDIS_KEY_CONTRIBUTORS);
    }

    /**
     * @param Contributor[] $contributors
     */
    private function setContributorsInCache($contributors): void
    {
        $this->redis->set(self::REDIS_KEY_CONTRIBUTORS, serialize($contributors));
    }

    /**
     * @param bool $setCache
     *
     * @return Contributor[]
     */
    private function getContributors($setCache = true): array
    {
        $contributors = [];

        $results = $this->getContributorsByGithub('PUGX', 'badge-poser');
        foreach ($results as $result) {
            $contributors[$result['login']] = Contributor::create($result['login'], $result['html_url'], $result['avatar_url']);
        }
        $results = $this->getContributorsByGithub('badges', 'poser');
        foreach ($results as $result) {
            $contributors[$result['login']] = Contributor::create($result['login'], $result['html_url'], $result['avatar_url']);
        }

        if ($setCache) {
            $this->setContributorsInCache($contributors);
        }

        return $contributors;
    }

    /**
     * @param $username
     * @param $repoName
     *
     * @return array
     */
    private function getContributorsByGithub($username, $repoName): array
    {
        $repoApi = $this->client->api('repo');
        $parameters = [$username, $repoName];
        $results = $this->resultPager->fetchAll(
            $repoApi,
            'contributors',
            $parameters
        );

        return $results;
    }
}

{'fraction_non_alphanumeric': 0.1386997957794418, 'fraction_numerical': 0.0, 'mean_word_length': 2.297979797979798, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3505797', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1663, 'n_words': 321}