Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
85 / 85
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
ReportController
100.00% covered (success)
100.00%
85 / 85
100.00% covered (success)
100.00%
6 / 6
22
100.00% covered (success)
100.00%
1 / 1
 create
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getRegionTitle
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 region
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
1 / 1
4
 getCityTitle
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 city
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3namespace Drupal\visitors_geoip\Controller;
4
5use Drupal\Core\Form\FormBuilderInterface;
6use Drupal\visitors\Controller\Report\ReportBaseController;
7use Drupal\visitors\VisitorsLocationInterface;
8use Symfony\Component\DependencyInjection\ContainerInterface;
9
10/**
11 * Report controller.
12 */
13class ReportController extends ReportBaseController {
14
15  /**
16   * The form builder.
17   *
18   * @var \Drupal\Core\Form\FormBuilderInterface
19   */
20  protected $formBuilder;
21
22  /**
23   * The location service.
24   *
25   * @var \Drupal\visitors\VisitorsLocationInterface
26   */
27  protected $location;
28
29  /**
30   * {@inheritdoc}
31   */
32  public static function create(ContainerInterface $container) {
33    return new self(
34      $container->get('form_builder'),
35      $container->get('visitors.location')
36    );
37  }
38
39  /**
40   * ReportController constructor.
41   *
42   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
43   *   The form builder.
44   * @param \Drupal\visitors\VisitorsLocationInterface $location
45   *   The location service.
46   */
47  public function __construct(FormBuilderInterface $form_builder, VisitorsLocationInterface $location) {
48    $this->formBuilder = $form_builder;
49    $this->location = $location;
50  }
51
52  /**
53   * The region page title.
54   */
55  public function getRegionTitle($country, $region) {
56    $title = 'Region';
57    if ($country) {
58      $title = $this->location->getCountryLabel($country);
59    }
60    if ($region) {
61      if ($region == '_none') {
62        $region = 'Unknown';
63      }
64      $title = "$region$title";
65    }
66    return $title;
67  }
68
69  /**
70   * Country report.
71   *
72   * @param string $country
73   *   The country code.
74   * @param string|null $region
75   *   The region code.
76   */
77  public function region($country, $region): array {
78    $args = [];
79    $view_display = 'region_table';
80    if ($country) {
81      $args[] = $country;
82    }
83    if ($region) {
84      if ($region != '_none') {
85        $args[] = $region;
86      }
87      $view_display = 'city_table';
88    }
89    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilter');
90    $first_row['region_table'] = [
91      '#view_id'      => 'visitors_geoip',
92      '#view_display' => $view_display,
93    ];
94
95    return [
96      'visitors_date_filter_form' => $form,
97
98      'main' => [
99        '#type' => 'container',
100        '#attributes' => [
101          'class' => ['visitors-main'],
102        ],
103        '1' => [
104          $this->renderViews($first_row, NULL, $args),
105        ],
106      ],
107      '#attached' => [
108        'library' => [
109          'visitors/visitors.report',
110        ],
111      ],
112    ];
113  }
114
115  /**
116   * The city page title.
117   */
118  public function getCityTitle($country, $region, $city) {
119    $title = 'City';
120    if ($country) {
121      $title = $this->location->getCountryLabel($country);
122    }
123    if ($region && $region != '_none') {
124      $title = "$region$title";
125    }
126    if ($city && $city != '_none') {
127      $title = "$city$title";
128    }
129
130    return $title;
131  }
132
133  /**
134   * City report.
135   *
136   * @param string|null $country
137   *   The country code.
138   * @param string|null $region
139   *   The region code.
140   * @param string|null $city
141   *   The city name.
142   */
143  public function city($country, $region, $city): array {
144
145    $args = [];
146    $view_display = 'city_table';
147    if ($country) {
148      $args[] = $country;
149    }
150    if ($region) {
151      if ($region = '_none') {
152        $args[] = NULL;
153      }
154      else {
155        $args[] = $region;
156      }
157    }
158    if ($city) {
159      if ($city = '_none') {
160        $args[] = NULL;
161      }
162      else {
163        $args[] = $city;
164      }
165      $view_display = 'recent_view_table';
166    }
167
168    $form = $this->formBuilder->getForm('Drupal\visitors\Form\DateFilter');
169    $first_row['region_table'] = [
170      '#view_id'      => 'visitors_geoip',
171      '#view_display' => $view_display,
172    ];
173
174    return [
175      'visitors_date_filter_form' => $form,
176
177      'main' => [
178        '#type' => 'container',
179        '#attributes' => [
180          'class' => ['visitors-main'],
181        ],
182        '1' => [
183          $this->renderViews($first_row, NULL, $args),
184        ],
185      ],
186      '#attached' => [
187        'library' => [
188          'visitors/visitors.report',
189        ],
190      ],
191    ];
192  }
193
194}