MobileTemplateFinder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the Madapaja.TwigModule package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Madapaja\TwigModule;
8
9
class MobileTemplateFinder implements TemplateFinderInterface
10
{
11
    const PHP_EXT = '.php';
12
13
    const MOBILE_EXT = '.mobile.twig';
14
15
    const TWIG_EXT = '.html.twig';
16
17
    private $userAgent;
18
19 3
    public function __construct($userAgent = null)
20
    {
21 3
        $this->userAgent = $userAgent;
22 3
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 2
    public function __invoke($name)
28
    {
29 2
        $pos = strrpos($name, self::PHP_EXT);
30 2
        $mobileFile = substr($name, 0, $pos) . self::MOBILE_EXT;
31 2
        $detect = new \Mobile_Detect(null, $this->userAgent);
32 2
        $isMobile = $detect->isMobile() && ! $detect->isTablet();
33 2
        if ($isMobile && file_exists($mobileFile)) {
34 1
            return $mobileFile;
35
        }
36 1
        $file = substr($name, 0, $pos) . self::TWIG_EXT;
37
38 1
        return $file;
39
    }
40
}
41