1313
1414namespace MobileDetectBundle \DataCollector ;
1515
16- use MobileDetectBundle \EventListener \RequestResponseListener ;
16+ use MobileDetectBundle \EventListener \RequestResponseListenerInterface ;
1717use MobileDetectBundle \Helper \DeviceView ;
18+ use MobileDetectBundle \Helper \DeviceViewInterface ;
1819use Symfony \Component \HttpFoundation \Request ;
1920use Symfony \Component \HttpFoundation \Response ;
2021use Symfony \Component \HttpKernel \DataCollector \DataCollector ;
2122
2223/**
2324 * @author Jonas HAOUZI <haouzijonas@gmail.com>
2425 */
25- class DeviceDataCollector extends DataCollector
26+ class DeviceDataCollector extends DataCollector implements DeviceDataCollectorInterface
2627{
2728 /**
2829 * @var DeviceView
@@ -50,68 +51,89 @@ public function collect(
5051 $ this ->data ['currentView ' ] = $ this ->deviceView ->getViewType ();
5152 $ this ->data ['views ' ] = [
5253 [
53- 'type ' => DeviceView ::VIEW_FULL ,
54+ 'type ' => DeviceViewInterface ::VIEW_FULL ,
5455 'label ' => 'Full ' ,
5556 'link ' => $ this ->generateSwitchLink (
5657 $ request ,
57- DeviceView ::VIEW_FULL
58+ DeviceViewInterface ::VIEW_FULL
5859 ),
5960 'isCurrent ' => $ this ->deviceView ->isFullView (),
60- 'enabled ' => $ this ->canUseView (DeviceView ::VIEW_FULL , $ request ->getSchemeAndHttpHost ()),
61+ 'enabled ' => $ this ->canUseView (DeviceViewInterface ::VIEW_FULL , $ request ->getSchemeAndHttpHost ()),
6162 ],
6263 [
63- 'type ' => DeviceView ::VIEW_TABLET ,
64+ 'type ' => DeviceViewInterface ::VIEW_TABLET ,
6465 'label ' => 'Tablet ' ,
6566 'link ' => $ this ->generateSwitchLink (
6667 $ request ,
67- DeviceView ::VIEW_TABLET
68+ DeviceViewInterface ::VIEW_TABLET
6869 ),
6970 'isCurrent ' => $ this ->deviceView ->isTabletView (),
70- 'enabled ' => $ this ->canUseView (DeviceView ::VIEW_TABLET , $ request ->getSchemeAndHttpHost ()),
71+ 'enabled ' => $ this ->canUseView (DeviceViewInterface ::VIEW_TABLET , $ request ->getSchemeAndHttpHost ()),
7172 ],
7273 [
73- 'type ' => DeviceView ::VIEW_MOBILE ,
74+ 'type ' => DeviceViewInterface ::VIEW_MOBILE ,
7475 'label ' => 'Mobile ' ,
7576 'link ' => $ this ->generateSwitchLink (
7677 $ request ,
77- DeviceView ::VIEW_MOBILE
78+ DeviceViewInterface ::VIEW_MOBILE
7879 ),
7980 'isCurrent ' => $ this ->deviceView ->isMobileView (),
80- 'enabled ' => $ this ->canUseView (DeviceView ::VIEW_MOBILE , $ request ->getSchemeAndHttpHost ()),
81+ 'enabled ' => $ this ->canUseView (DeviceViewInterface ::VIEW_MOBILE , $ request ->getSchemeAndHttpHost ()),
8182 ],
8283 ];
8384 }
8485
86+ /**
87+ * Get the current view being displayed.
88+ */
8589 public function getCurrentView (): string
8690 {
8791 return $ this ->data ['currentView ' ];
8892 }
8993
94+ /**
95+ * Gets the views from the data array.
96+ */
9097 public function getViews (): array
9198 {
9299 return $ this ->data ['views ' ];
93100 }
94101
102+ /**
103+ * Sets the redirect configuration.
104+ */
95105 public function setRedirectConfig (array $ redirectConfig ): void
96106 {
97107 $ this ->redirectConfig = $ redirectConfig ;
98108 }
99109
110+ /**
111+ * Get the name of the collector device.
112+ */
100113 public function getName (): string
101114 {
102115 return 'device.collector ' ;
103116 }
104117
118+ /**
119+ * Gets the data.
120+ */
105121 public function getData (): array
106122 {
107123 return $ this ->data ;
108124 }
109125
126+ /**
127+ * Resets the data to an empty array.
128+ */
110129 public function reset (): void
111130 {
112131 $ this ->data = [];
113132 }
114133
134+ /**
135+ * Check if the view can be used based on configuration settings.
136+ */
115137 protected function canUseView (string $ view , ?string $ host ): bool
116138 {
117139 if (!\is_array ($ this ->redirectConfig )
@@ -125,7 +147,7 @@ protected function canUseView(string $view, ?string $host): bool
125147 if (true === $ this ->redirectConfig [$ view ]['is_enabled ' ]
126148 && isset ($ this ->redirectConfig [$ view ]['host ' ], $ this ->redirectConfig [$ view ]['action ' ])
127149 && !empty ($ this ->redirectConfig [$ view ]['host ' ])
128- && \in_array ($ this ->redirectConfig [$ view ]['action ' ], [RequestResponseListener ::REDIRECT , RequestResponseListener ::REDIRECT_WITHOUT_PATH ], true )
150+ && \in_array ($ this ->redirectConfig [$ view ]['action ' ], [RequestResponseListenerInterface ::REDIRECT , RequestResponseListenerInterface ::REDIRECT_WITHOUT_PATH ], true )
129151 ) {
130152 $ parseHost = parse_url ((string ) $ this ->redirectConfig [$ view ]['host ' ]);
131153 $ redirectHost = $ parseHost ['scheme ' ].':// ' .$ parseHost ['host ' ];
@@ -141,6 +163,9 @@ protected function canUseView(string $view, ?string $host): bool
141163 return true ;
142164 }
143165
166+ /**
167+ * Generates a switch link for switching the view based on the provided view parameter.
168+ */
144169 private function generateSwitchLink (
145170 Request $ request ,
146171 string $ view ,
0 commit comments