<commit_before><?php

namespace ZfModule\Controller;

use Application\Service\RepositoryRetriever;
use EdpGithub\Client;
use EdpGithub\Collection\RepositoryCollection;
use Zend\Cache;
use Zend\Http;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use ZfModule\Mapper;
use ZfModule\Service;

class IndexController extends AbstractActionController
{
    /**
     * @var Cache\Storage\StorageInterface
     */
    private $moduleCache;

    /**
     * @var Mapper\Module
     */
    private $moduleMapper;

    /**
     * @var Service\Module
     */
    private $moduleService;

    /**
     * @var Client
     */
    private $githubClient;

    /**
     * @var RepositoryRetriever
     */
    private $repositoryRetriever;

    /**
     * @param Cache\Storage\StorageInterface $moduleCache
     * @param Mapper\Module $moduleMapper
     * @param Service\Module $moduleService
     * @param Client $githubClient
     * @param RepositoryRetriever $repositoryRetriever
     */
    public function __construct(
        Cache\Storage\StorageInterface $moduleCache,
        Mapper\Module $moduleMapper,
        Service\Module $moduleService,
        Client $githubClient,
        RepositoryRetriever $repositoryRetriever
    ) {
        $this->moduleCache = $moduleCache;
        $this->moduleMapper = $moduleMapper;
        $this->moduleService = $moduleService;
        $this->githubClient = $githubClient;
        $this->repositoryRetriever = $repositoryRetriever;
    }

    public function viewAction()
    {
        $vendor = $this->params()->fromRoute('vendor', null);
        $module = $this->params()->fromRoute('module', null);

        $result = $this->moduleMapper->findByName($module);
        if (!$result) {
            $this->getResponse()->setStatusCode(Http\Response::STATUS_CODE_404);
            return;
        }

        $repositoryCacheKey = 'module-view-' . $vendor . '-' . $module;
        $repository = $this->repositoryRetriever->getUserRepositoryMetadata($vendor, $module);

        $httpClient = $this->githubClient->getHttpClient();
        $response= $httpClient->getResponse();
        if ($response->getStatusCode() == Http\Response::STATUS_CODE_304 && $this->moduleCache->hasItem($repositoryCacheKey)) {
            return $this->moduleCache->getItem($repositoryCacheKey);
        }

        $readme = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'README.md');
        $license = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'LICENSE');
        $license = $license === false ? 'No license file found for this Module' : $license;

        $composerConf = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'composer.json');
        $composerConf = $composerConf === false ? 'No composer.json file found for this Module' : json_decode($composerConf, true);

        $viewModel = new ViewModel(array(
            'vendor' => $vendor,
            'module' => $module,
            'repository' => $repository,
            'readme' => $readme,
            'composerConf' => $composerConf,
            'license' => $license,
        ));

        $this->moduleCache->setItem($repositoryCacheKey, $viewModel);

        return $viewModel;
    }

    public function indexAction()
    {
        if (!$this->zfcUserAuthentication()->hasIdentity()) {
            return $this->redirect()->toRoute('zfcuser/login');
        }

        $params = array(
            'type'      => 'all',
            'per_page'  => 100,
            'sort'      => 'updated',
            'direction' => 'desc',
        );

        /* @var RepositoryCollection $repos */
        $repos = $this->repositoryRetriever->getAuthUserRepositories($params);

        $identity = $this->zfcUserAuthentication()->getIdentity();
        $cacheKey = 'modules-user-' . $identity->getId();

        $repositories = $this->fetchModules($repos, $cacheKey);

        $viewModel = new ViewModel(array('repositories' => $repositories));
        $viewModel->setTerminal(true);
        return $viewModel;
    }

    public function organizationAction()
    {
        if (!$this->zfcUserAuthentication()->hasIdentity()) {
            return $this->redirect()->toRoute('zfcuser/login');
        }

        $owner = $this->params()->fromRoute('owner', null);
        $params = array(
            'per_page'  => 100,
            'sort'      => 'updated',
            'direction' => 'desc',
        );

        /* @var RepositoryCollection $repos */
        $repos = $this->repositoryRetriever->getUserRepositories($owner, $params);

        $identity = $this->zfcUserAuthentication()->getIdentity();
        $cacheKey = 'modules-organization-' . $identity->getId() . '-' . $owner;

        $repositories = $this->fetchModules($repos, $cacheKey);
        $viewModel = new ViewModel(array('repositories' => $repositories));
        $viewModel->setTerminal(true);
        $viewModel->setTemplate('zf-module/index/index.phtml');
        return $viewModel;
    }

    /**
     * @param \Generator $repos
     * @param string $cacheKey
     * @return array
     */
    public function fetchModules(\Generator $repos, $cacheKey)
    {
        $cacheKey .= '-github';

        $repositories = array();

        foreach ($repos as $repo) {
            $isModule = $this->moduleService->isModule($repo);
            //Verify if repos have been modified
            $httpClient = $this->githubClient->getHttpClient();
            /* @var $response \Zend\Http\Response */<commit_msg>rename method<commit_after><?php

namespace ZfModule\Controller;

use Application\Service\RepositoryRetriever;
use EdpGithub\Client;
use EdpGithub\Collection\RepositoryCollection;
use Zend\Cache;
use Zend\Http;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use ZfModule\Mapper;
use ZfModule\Service;

class IndexController extends AbstractActionController
{
    /**
     * @var Cache\Storage\StorageInterface
     */
    private $moduleCache;

    /**
     * @var Mapper\Module
     */
    private $moduleMapper;

    /**
     * @var Service\Module
     */
    private $moduleService;

    /**
     * @var Client
     */
    private $githubClient;

    /**
     * @var RepositoryRetriever
     */
    private $repositoryRetriever;

    /**
     * @param Cache\Storage\StorageInterface $moduleCache
     * @param Mapper\Module $moduleMapper
     * @param Service\Module $moduleService
     * @param Client $githubClient
     * @param RepositoryRetriever $repositoryRetriever
     */
    public function __construct(
        Cache\Storage\StorageInterface $moduleCache,
        Mapper\Module $moduleMapper,
        Service\Module $moduleService,
        Client $githubClient,
        RepositoryRetriever $repositoryRetriever
    ) {
        $this->moduleCache = $moduleCache;
        $this->moduleMapper = $moduleMapper;
        $this->moduleService = $moduleService;
        $this->githubClient = $githubClient;
        $this->repositoryRetriever = $repositoryRetriever;
    }

    public function viewAction()
    {
        $vendor = $this->params()->fromRoute('vendor', null);
        $module = $this->params()->fromRoute('module', null);

        $result = $this->moduleMapper->findByName($module);
        if (!$result) {
            $this->getResponse()->setStatusCode(Http\Response::STATUS_CODE_404);
            return;
        }

        $repositoryCacheKey = 'module-view-' . $vendor . '-' . $module;
        $repository = $this->repositoryRetriever->getUserRepositoryMetadata($vendor, $module);

        $httpClient = $this->githubClient->getHttpClient();
        $response= $httpClient->getResponse();
        if ($response->getStatusCode() == Http\Response::STATUS_CODE_304 && $this->moduleCache->hasItem($repositoryCacheKey)) {
            return $this->moduleCache->getItem($repositoryCacheKey);
        }

        $readme = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'README.md');
        $license = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'LICENSE');
        $license = $license === false ? 'No license file found for this Module' : $license;

        $composerConf = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'composer.json');
        $composerConf = $composerConf === false ? 'No composer.json file found for this Module' : json_decode($composerConf, true);

        $viewModel = new ViewModel(array(
            'vendor' => $vendor,
            'module' => $module,
            'repository' => $repository,
            'readme' => $readme,
            'composerConf' => $composerConf,
            'license' => $license,
        ));

        $this->moduleCache->setItem($repositoryCacheKey, $viewModel);

        return $viewModel;
    }

    public function indexAction()
    {
        if (!$this->zfcUserAuthentication()->hasIdentity()) {
            return $this->redirect()->toRoute('zfcuser/login');
        }

        $params = array(
            'type'      => 'all',
            'per_page'  => 100,
            'sort'      => 'updated',
            'direction' => 'desc',
        );

        /* @var RepositoryCollection $repos */
        $repos = $this->repositoryRetriever->getAuthenticatedUserRepositories($params);

        $identity = $this->zfcUserAuthentication()->getIdentity();
        $cacheKey = 'modules-user-' . $identity->getId();

        $repositories = $this->fetchModules($repos, $cacheKey);

        $viewModel = new ViewModel(array('repositories' => $repositories));
        $viewModel->setTerminal(true);
        return $viewModel;
    }

    public function organizationAction()
    {
        if (!$this->zfcUserAuthentication()->hasIdentity()) {
            return $this->redirect()->toRoute('zfcuser/login');
        }

        $owner = $this->params()->fromRoute('owner', null);
        $params = array(
            'per_page'  => 100,
            'sort'      => 'updated',
            'direction' => 'desc',
        );

        /* @var RepositoryCollection $repos */
        $repos = $this->repositoryRetriever->getUserRepositories($owner, $params);

        $identity = $this->zfcUserAuthentication()->getIdentity();
        $cacheKey = 'modules-organization-' . $identity->getId() . '-' . $owner;

        $repositories = $this->fetchModules($repos, $cacheKey);
        $viewModel = new ViewModel(array('repositories' => $repositories));
        $viewModel->setTerminal(true);
        $viewModel->setTemplate('zf-module/index/index.phtml');
        return $viewModel;
    }

    /**
     * @param \Generator $repos
     * @param string $cacheKey
     * @return array
     */
    public function fetchModules($repos, $cacheKey)
    {
        $cacheKey .= '-github';

        $repositories = array();

        foreach ($repos as $repo) {
            $isModule = $this->moduleService->isModule($repo);
            //Verify if repos have been modified
            $httpClient = $this->githubClient->getHttpClient();
            /* @var $response \Zend\Http\Response */
{'fraction_non_alphanumeric': 0.14392708897310955, 'fraction_numerical': 0.002165674066053059, 'mean_word_length': 2.6148075668623614, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 153, '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': '254865', 'n_tokens_mistral': 3128, 'n_tokens_neox': 3032, 'n_words': 562}