<?php

/**
 * seo_serp_preview extension for Contao Open Source CMS
 *
 * Copyright (C) 2016 derhaeuptling
 *
 * @author  derhaeuptling <https://derhaeuptling.com>
 * @author  Codefog <http://codefog.pl>
 * @author  Kamil Kuzminski <*****@*****.**>
 * @license LGPL
 */
/**
 * Backend module
 */
$GLOBALS['BE_MOD']['content']['seo_serp_preview'] = ['icon' => 'system/modules/seo_serp_preview/assets/icons/module.png', 'callback' => 'Derhaeuptling\\SeoSerpPreview\\PreviewModule'];
/**
 * Backend form fields
 */
$GLOBALS['BE_FFL']['seoSerpPreview'] = 'Derhaeuptling\\SeoSerpPreview\\PreviewWidget';
/**
 * Hooks
 */
$GLOBALS['TL_HOOKS']['getUserNavigation'][] = ['Derhaeuptling\\SeoSerpPreview\\StatusManager', 'setMenuStatus'];
/**
 * SEO SERP Tests
 */
\Derhaeuptling\SeoSerpPreview\TestsManager::add('description', 'Derhaeuptling\\SeoSerpPreview\\Test\\DescriptionTest');
 /**
  * Run the tests
  *
  * @param string $table
  * @param array  $data
  *
  * @throws ErrorException
  * @throws WarningException
  */
 protected function runTests($table, array $data)
 {
     /** @var TestInterface $test */
     foreach (TestsManager::getAll() as $test) {
         if (!$test->supports($table)) {
             continue;
         }
         $test->run($data, $table);
     }
 }
 /**
  * Run the tests on a table
  *
  * @param string $table
  * @param Result $records
  *
  * @return array
  */
 protected function runTests($table, Result $records)
 {
     System::loadLanguageFile('seo_serp_tests');
     $result = ['errors' => 0, 'warnings' => 0];
     /** @var TestInterface $test */
     foreach (TestsManager::getAll() as $test) {
         // Skip the unsupported tests
         if (!$test->supports($table)) {
             continue;
         }
         while ($records->next()) {
             try {
                 $test->run($records->row(), $table);
             } catch (ErrorException $e) {
                 $result['errors']++;
             } catch (WarningException $e) {
                 $result['warnings']++;
             }
         }
         $records->reset();
     }
     return $result;
 }
 /**
  * Generate the tests for a record
  *
  * @param array $data
  *
  * @return array
  */
 protected function generateTests(array $data)
 {
     System::loadLanguageFile('seo_serp_tests');
     $result = ['errors' => [], 'warnings' => []];
     /** @var TestInterface $test */
     foreach (TestsManager::getAll() as $test) {
         // Skip the unsupported tests
         if (!$test->supports($this->table)) {
             continue;
         }
         try {
             $test->run($data, $this->table);
         } catch (ErrorException $e) {
             $result['errors'][] = $e->getMessage();
         } catch (WarningException $e) {
             $result['warnings'][] = $e->getMessage();
         }
     }
     return $result;
 }