<commit_before><?php

/*
 * (c) Captain Courier Integration <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Cidr;

use Cidr\Exception\FileNotFoundException;
use Cidr\Model\Task;
use Cidr\CourierPluginMetadata;
use Cidr\Exception\IllegalStateException;

class CourierPluginDetector
{ use Milk;

    private $courierFolder;
    private $namespace;
    private $validationPostfix;

    /** @var list of configuration files to look for */
    private $configurationFileNames;

    private $courierMetadataFactory;

    private $requireAllCouriersFeatureComplete;

    /**
     * TODO CLEAN UP
     * @return CourierIntegration[] detected
     */
    public function detectCouriers()
    {
        $couriers = [];
        foreach ($this->getCourierFolders() as $courierName) {

            $validationFileNames = [];
            foreach (Task::$Tasks as $task) {
                $fileName = "{$this->courierFolder}/{$courierName}/{$task}{$this->validationPostfix}";
                if($this->requireAllCouriersFeatureComplete and !file_exists($fileName)) {
                    throw new FileNotFoundException($fileName);
                }
                $validationFileNames[$task] = file_exists($fileName) ? $fileName : null;

            }

            $configurationResources = [];
            foreach($this->configurationFileNames as $canonicalConfigurationFileName) {
                $configurationFileName = "{$this->courierFolder}/{$courierName}/{$canonicalConfigurationFileName}";

                if(!file_exists($configurationFileName)) {
                    continue;
                }

                if (\Cidr\endsWith($configurationFileName, ".php")) {
                    $resourceName = "{$this->namespace}\\{$courierName}\\" . explode (".", $canonicalConfigurationFileName)[0];
                    $resourceType = CourierPluginMetadata::RESOURCE_CLASS;
                } else if(\cidr\endsWith($configurationFileName, ".yml")) {
                    $resourceName = $configurationFileName;
                    $resourceType = CourierPluginMetadata::RESOURCE_YAML;
                } else {
                    throw new IllegalStateException(sprintf("unable to handle configuration file '%s', can handle the following file types: php, yml"));
                }
                $configurationResources[$resourceName] = $resourceType;
            }

            if(count($configurationResources) == 0 ) {
                throw new IllegalStateException(sprintf("courier plugin for courier '%s' requires at least one configuration file", $courierName));
            }

            $couriers[] = $this->courierMetadataFactory->create(
                $courierName,
                $configurationResources,                    
                $validationFileNames
            );
        }

        return $couriers;

    }

    private function getCourierFolders()
    {
        $handle = opendir ($this->courierFolder);
        while (false !== ($entry = readdir ($handle))) {<commit_msg>fully qualify yaml resource names upon detection<commit_after><?php

/*
 * (c) Captain Courier Integration <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Cidr;

use Cidr\Exception\FileNotFoundException;
use Cidr\Model\Task;
use Cidr\CourierPluginMetadata;
use Cidr\Exception\IllegalStateException;

class CourierPluginDetector
{ use Milk;

    private $courierFolder;
    private $namespace;
    private $validationPostfix;

    /** @var list of configuration files to look for */
    private $configurationFileNames;

    private $courierMetadataFactory;

    private $requireAllCouriersFeatureComplete;

    /**
     * TODO CLEAN UP
     * @return CourierIntegration[] detected
     */
    public function detectCouriers()
    {
        $couriers = [];
        foreach ($this->getCourierFolders() as $courierName) {

            $validationFileNames = [];
            foreach (Task::$Tasks as $task) {
                $fileName = "{$this->courierFolder}/{$courierName}/{$task}{$this->validationPostfix}";
                if($this->requireAllCouriersFeatureComplete and !file_exists($fileName)) {
                    throw new FileNotFoundException($fileName);
                }
                $validationFileNames[$task] = file_exists($fileName) ? $fileName : null;

            }

            $configurationResources = [];
            foreach($this->configurationFileNames as $canonicalConfigurationFileName) {
                $configurationFileName = "{$this->courierFolder}/{$courierName}/{$canonicalConfigurationFileName}";

                if(!file_exists($configurationFileName)) {
                    continue;
                }

                if (\Cidr\endsWith($configurationFileName, ".php")) {
                    $resourceName = "{$this->namespace}\\{$courierName}\\" . explode (".", $canonicalConfigurationFileName)[0];
                    $resourceType = CourierPluginMetadata::RESOURCE_CLASS;
                } else if(\cidr\endsWith($configurationFileName, ".yml")) {
                    $resourceName = realpath($configurationFileName);
                    $resourceType = CourierPluginMetadata::RESOURCE_YAML;
                } else {
                    throw new IllegalStateException(sprintf("unable to handle configuration file '%s', can handle the following file types: php, yml"));
                }
                $configurationResources[$resourceName] = $resourceType;
            }

            if(count($configurationResources) == 0 ) {
                throw new IllegalStateException(sprintf("courier plugin for courier '%s' requires at least one configuration file", $courierName));
            }

            $couriers[] = $this->courierMetadataFactory->create(
                $courierName,
                $configurationResources,                    
                $validationFileNames
            );
        }

        return $couriers;

    }

    private function getCourierFolders()
    {
        $handle = opendir ($this->courierFolder);
        while (false !== ($entry = readdir ($handle))) {
{'fraction_non_alphanumeric': 0.10846264597664373, 'fraction_numerical': 0.0006398976163813789, 'mean_word_length': 2.3096876654314453, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, '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': '3933324', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1496, 'n_words': 340}