Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
15 / 15
DeviceDetectorService
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
15 / 15
 factory
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
8 / 8
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 detectDeviceProperties
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
<?php
namespace MobileFrontend\Devices;
use Config;
use WebRequest;
/**
 * MobileFrontend's device detector.
 *
 * Detects the properties of a device by iterating over a list, //in order//,
 * child device detectors until one returns a positive result.
 *
 * The order of the child device detectors is defined in
 * `DeviceDetectorService::factory`.
 */
class DeviceDetectorService implements DeviceDetector {
    /**
     * Given MobileFrontend's configuration, creates a new instance of the device
     * detector.
     *
     * If `$wgMFAutodetectMobileView` is falsy, then no device detection will
     * occur.
     *
     * @param Config $config containing values for MFAutodetectMobileView and MFMobileHeader
     * @return DeviceDetectorService
     */
    public static function factory( Config $config ) {
        $children = [];
        if ( $config->get( 'MFAutodetectMobileView' ) ) {
            array_push(
                $children,
                new AMFDeviceDetector(),
                new CustomHeaderDeviceDetector( $config ),
                new UADeviceDetector()
            );
        }
        return new self( $children );
    }
    /**
     * @var DeviceDetector[]
     */
    private $children;
    /**
     * @param DeviceDetector[] $children
     */
    public function __construct( array $children ) {
        $this->children = $children;
    }
    /**
     * @inheritDoc
     */
    public function detectDeviceProperties( WebRequest $request, array $server ) {
        foreach ( $this->children as $child ) {
            $properties = $child->detectDeviceProperties( $request, $server );
            if ( $properties ) {
                return $properties;
            }
        }
    }
}