Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | namespace Drupal\visitors\Controller; | |
| 4 | ||
| 5 | use Drupal\Core\Controller\ControllerBase; | |
| 6 | use Drupal\Core\Extension\ModuleHandlerInterface; | |
| 7 | use Drupal\Core\Form\FormBuilderInterface; | |
| 8 | use Drupal\Core\Messenger\MessengerInterface; | |
| 9 | use Drupal\Core\Url; | |
| 10 | use Symfony\Component\DependencyInjection\ContainerInterface; | |
| 11 | use Symfony\Component\HttpFoundation\RedirectResponse; | |
| 12 | ||
| 13 | /** | |
| 14 | * Controller for the statistics migration form. | |
| 15 | */ | |
| 16 | class StatisticsMigrateController extends ControllerBase { | |
| 17 | ||
| 18 | /** | |
| 19 | * The module handler. | |
| 20 | * | |
| 21 | * @var \Drupal\Core\Extension\ModuleHandlerInterface | |
| 22 | */ | |
| 23 | protected $moduleHandler; | |
| 24 | ||
| 25 | /** | |
| 26 | * The form builder. | |
| 27 | * | |
| 28 | * @var \Drupal\Core\Form\FormBuilderInterface | |
| 29 | */ | |
| 30 | protected $formBuilder; | |
| 31 | ||
| 32 | /** | |
| 33 | * Constructs the counter service. | |
| 34 | * | |
| 35 | * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler | |
| 36 | * The module handler. | |
| 37 | * @param \Drupal\Core\Form\FormBuilderInterface $form_builder | |
| 38 | * The form builder. | |
| 39 | * @param \Drupal\Core\Messenger\MessengerInterface $messenger | |
| 40 | * The messenger. | |
| 41 | */ | |
| 42 | public function __construct(ModuleHandlerInterface $module_handler, FormBuilderInterface $form_builder, MessengerInterface $messenger) { | |
| 43 | 4 | $this->moduleHandler = $module_handler; |
| 44 | 4 | $this->formBuilder = $form_builder; |
| 45 | 4 | $this->setMessenger($messenger); |
| 46 | } | |
| 47 | ||
| 48 | /** | |
| 49 | * {@inheritdoc} | |
| 50 | */ | |
| 51 | public static function create(ContainerInterface $container) { | |
| 52 | 4 | return new static( |
| 53 | 4 | $container->get('module_handler'), |
| 54 | 4 | $container->get('form_builder'), |
| 55 | 4 | $container->get('messenger'), |
| 56 | 4 | ); |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Migrate statistics. | |
| 61 | */ | |
| 62 | public function migrate() { | |
| 63 | ||
| 64 | 2 | $statistics_is_installed = $this->moduleHandler->moduleExists('statistics'); |
| 65 | 2 | if (!$statistics_is_installed) { |
| 66 | 1 | $this->messenger()->addWarning('The Statistics module is not installed.'); |
| 67 | 1 | $url = Url::fromRoute('visitors.settings'); |
| 68 | 1 | $response = new RedirectResponse($url->toString()); |
| 69 | 1 | return $response->send(); |
| 70 | } | |
| 71 | ||
| 72 | 1 | $form = $this->formBuilder->getForm('Drupal\visitors\Form\StatisticsMigrateForm'); |
| 73 | 1 | return $form; |
| 74 | } | |
| 75 | ||
| 76 | } |