Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
DeviceDetectorService
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 factory
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 detectDeviceProperties
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace MobileFrontend\Devices;
4
5use MediaWiki\Config\Config;
6use MediaWiki\Request\WebRequest;
7
8/**
9 * MobileFrontend's device detector.
10 *
11 * Detects the properties of a device by iterating over a list, //in order//,
12 * child device detectors until one returns a positive result.
13 *
14 * The order of the child device detectors is defined in
15 * `DeviceDetectorService::factory`.
16 */
17class DeviceDetectorService implements DeviceDetector {
18
19    /**
20     * Given MobileFrontend's configuration, creates a new instance of the device
21     * detector.
22     *
23     * If `$wgMFAutodetectMobileView` is falsy, then no device detection will
24     * occur.
25     *
26     * @param Config $config containing values for MFAutodetectMobileView and MFMobileHeader
27     * @return self
28     */
29    public static function factory( Config $config ) {
30        $children = [];
31
32        if ( $config->get( 'MFAutodetectMobileView' ) ) {
33            array_push(
34                $children,
35                new AMFDeviceDetector(),
36                new CustomHeaderDeviceDetector( $config ),
37                new UADeviceDetector()
38            );
39        }
40
41        return new self( $children );
42    }
43
44    /**
45     * @param DeviceDetector[] $children
46     */
47    public function __construct(
48        private readonly array $children,
49    ) {
50    }
51
52    /**
53     * @inheritDoc
54     */
55    public function detectDeviceProperties( WebRequest $request, array $server ) {
56        foreach ( $this->children as $child ) {
57            $properties = $child->detectDeviceProperties( $request, $server );
58
59            if ( $properties ) {
60                return $properties;
61            }
62        }
63    }
64}