You are here

class AcquiaCohesionDataSuggester in TMGMT Translator Smartling 8.4

Hierarchy

Expanded class hierarchy of AcquiaCohesionDataSuggester

1 file declares its use of AcquiaCohesionDataSuggester
AcquiaCohesionDataSuggesterTest.php in modules/tmgmt_smartling_acquia_cohesion/tests/src/Kernel/AcquiaCohesionDataSuggesterTest.php
1 string reference to 'AcquiaCohesionDataSuggester'
tmgmt_smartling_acquia_cohesion.services.yml in modules/tmgmt_smartling_acquia_cohesion/tmgmt_smartling_acquia_cohesion.services.yml
modules/tmgmt_smartling_acquia_cohesion/tmgmt_smartling_acquia_cohesion.services.yml
1 service uses AcquiaCohesionDataSuggester
tmgmt_smartling_acquia_cohesion.data_suggester in modules/tmgmt_smartling_acquia_cohesion/tmgmt_smartling_acquia_cohesion.services.yml
\Drupal\tmgmt_smartling_acquia_cohesion\AcquiaCohesionDataSuggester

File

modules/tmgmt_smartling_acquia_cohesion/src/AcquiaCohesionDataSuggester.php, line 9

Namespace

Drupal\tmgmt_smartling_acquia_cohesion
View source
class AcquiaCohesionDataSuggester implements LayoutCanvasAwareInterface {
  use LayoutCanvasAwareTrait;

  /**
   * @var LoggerInterface
   */
  protected $logger;

  /**
   * AcquiaCohesionDataSuggester constructor.
   * @param LoggerInterface $logger
   */
  public function __construct(LoggerInterface $logger) {
    $this->logger = $logger;
  }

  /**
   * @param array $jobItems
   * @return array
   */
  public function suggestCohesionContentComponents(array $jobItems) {
    $suggestions = [];
    foreach ($jobItems as $jobItem) {
      $jobItemData = $jobItem
        ->getData();
      $suggestions = array_merge($suggestions, $this
        ->walkRecursiveAndGetSuggestions($jobItemData, $jobItem));
    }
    return $suggestions;
  }

  /**
   * Only implemented manually because phpunit fails to run tests with closures.
   *
   * This recursion could be easily rewritten with array_walk_recursive
   * with closure but phpunit serializes data for process isolation purposes
   * which causes exception because closure can not be serialized.
   *
   * @param mixed $data
   * @param JobItemInterface $jobItem
   * @return array
   */
  protected function walkRecursiveAndGetSuggestions($data, JobItemInterface $jobItem) {
    $suggestions = [];
    if (is_string($data) && ($layoutCanvas = $this
      ->isLayoutCanvas($data))) {
      foreach ($layoutCanvas
        ->iterateCanvas() as $element) {
        if ($element
          ->isComponentContent()) {

          // This is the way Acquia Cohesion module uses for fetching number
          // from componentId property so used here ias well.
          $id = str_replace("cc_", "", $element
            ->getProperty("componentContentId"));
          $componentContent = $this
            ->loadComponentContent($id);
          if (empty($componentContent)) {
            $this
              ->logger("Tried to suggest component content id=@id but failed to load it", [
              "@id" => $id,
            ]);
            continue;
          }
          $suggestions[] = [
            'job_item' => tmgmt_job_item_create('content', $componentContent
              ->getEntityTypeId(), $componentContent
              ->id()),
            'reason' => t('Referenced content component'),
            'from_item' => $jobItem
              ->id(),
          ];
        }
      }
    }
    if (is_array($data)) {
      foreach ($data as $key => $value) {
        $suggestions += $this
          ->walkRecursiveAndGetSuggestions($value, $jobItem);
      }
    }
    return $suggestions;
  }

  /**
   * Needed for testing.
   *
   * @param int $id
   * @return ComponentContent|null
   */
  protected function loadComponentContent($id) {
    return ComponentContent::load($id);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcquiaCohesionDataSuggester::$logger protected property
AcquiaCohesionDataSuggester::loadComponentContent protected function Needed for testing.
AcquiaCohesionDataSuggester::suggestCohesionContentComponents public function
AcquiaCohesionDataSuggester::walkRecursiveAndGetSuggestions protected function Only implemented manually because phpunit fails to run tests with closures.
AcquiaCohesionDataSuggester::__construct public function AcquiaCohesionDataSuggester constructor.
LayoutCanvasAwareTrait::isLayoutCanvas public function