Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
4 / 4 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CustomHeaderDeviceDetector | |
100.00% |
4 / 4 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| detectDeviceProperties | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MobileFrontend\Devices; |
| 4 | |
| 5 | use MediaWiki\Config\Config; |
| 6 | use MediaWiki\Request\WebRequest; |
| 7 | |
| 8 | /** |
| 9 | * Detects mobile devices by testing whether a custom request header is present. |
| 10 | * |
| 11 | * @note See README.md for more detail on `$wgMFMobileHeader`. |
| 12 | */ |
| 13 | class CustomHeaderDeviceDetector implements DeviceDetector { |
| 14 | |
| 15 | /** |
| 16 | * The name of the custom request header. |
| 17 | */ |
| 18 | private readonly ?string $customHeaderName; |
| 19 | |
| 20 | /** |
| 21 | * @param Config $config The global config. Currently this can be any instance |
| 22 | * of `GlobalVarConfig`. |
| 23 | * |
| 24 | * @todo In future, however, this should probably be a MobileFrontend-specific |
| 25 | * instance. `GlobalVarConfig#__construct` accepts a custom prefix to avoid |
| 26 | * repeating prefixes in `#get` calls, e.g. |
| 27 | * |
| 28 | * ``` |
| 29 | * $config = new GlobalVarConfig(); |
| 30 | * $mobileFrontendConfig = new GlobalVarConfig( 'wgMF' ); |
| 31 | * |
| 32 | * assert( |
| 33 | * $config->get( 'MFMobileHeader' ) |
| 34 | * === $mobileFrontendConfig->get( 'MobileHeader' ) |
| 35 | * ); |
| 36 | * ``` |
| 37 | */ |
| 38 | public function __construct( Config $config ) { |
| 39 | $this->customHeaderName = $config->get( 'MFMobileHeader' ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @inheritDoc |
| 44 | */ |
| 45 | public function detectDeviceProperties( WebRequest $request, array $server ) { |
| 46 | if ( |
| 47 | $this->customHeaderName |
| 48 | && $request->getHeader( $this->customHeaderName ) !== false |
| 49 | ) { |
| 50 | return new DeviceProperties( true, false ); |
| 51 | } |
| 52 | } |
| 53 | } |