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     * @var DeviceDetector[]
46     */
47    private $children;
48
49    /**
50     * @param DeviceDetector[] $children
51     */
52    public function __construct( array $children ) {
53        $this->children = $children;
54    }
55
56    /**
57     * @inheritDoc
58     */
59    public function detectDeviceProperties( WebRequest $request, array $server ) {
60        foreach ( $this->children as $child ) {
61            $properties = $child->detectDeviceProperties( $request, $server );
62
63            if ( $properties ) {
64                return $properties;
65            }
66        }
67    }
68}