gotem_content_moderation-1.1.6-alpha1/src/service/ThresholdService.php

src/service/ThresholdService.php
<?php

namespace Drupal\gotem\Service;

use Drupal\Core\Config\ConfigFactoryInterface;

/**
 * Class ThresholdService.
 *
 * Handles moderation thresholds for multiple categories.
 */
class ThresholdService {

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a ThresholdService object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * Retrieves the moderation threshold for a given category.
   *
   * @param string $category
   *   The category (e.g., 'hate', 'violence').
   *
   * @return float
   *   The moderation threshold for the category.
   */
  public function getThreshold(string $category): float {
    $config = $this->configFactory->get('gotem.settings');
    $default_threshold = 0.5; // Default threshold for categories
    return (float) $config->get("moderation_thresholds.$category") ?? $default_threshold;
  }

  /**
   * Determines if the content should be flagged based on the moderation score for a category.
   *
   * @param float $score
   *   The moderation score from the API.
   * @param string $category
   *   The moderation category (e.g., 'hate', 'violence').
   *
   * @return bool
   *   TRUE if the content should be flagged, FALSE otherwise.
   */
  public function shouldFlagContent(float $score, string $category): bool {
    $threshold = $this->getThreshold($category);
    return $score >= $threshold;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc