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 | * @var string |
19 | */ |
20 | private $customHeaderName; |
21 | |
22 | /** |
23 | * @param Config $config The global config. Currently this can be any instance |
24 | * of `GlobalVarConfig`. |
25 | * |
26 | * @todo In future, however, this should probably be a MobileFrontend-specific |
27 | * instance. `GlobalVarConfig#__construct` accepts a custom prefix to avoid |
28 | * repeating prefixes in `#get` calls, e.g. |
29 | * |
30 | * ``` |
31 | * $config = new GlobalVarConfig(); |
32 | * $mobileFrontendConfig = new GlobalVarConfig( 'wgMF' ); |
33 | * |
34 | * assert( |
35 | * $config->get( 'MFMobileHeader' ) |
36 | * === $mobileFrontendConfig->get( 'MobileHeader' ) |
37 | * ); |
38 | * ``` |
39 | */ |
40 | public function __construct( Config $config ) { |
41 | $this->customHeaderName = $config->get( 'MFMobileHeader' ); |
42 | } |
43 | |
44 | /** |
45 | * @inheritDoc |
46 | */ |
47 | public function detectDeviceProperties( WebRequest $request, array $server ) { |
48 | if ( |
49 | $this->customHeaderName |
50 | && $request->getHeader( $this->customHeaderName ) !== false |
51 | ) { |
52 | return new DeviceProperties( true, false ); |
53 | } |
54 | } |
55 | } |