MobileTemplateFinder::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 9
nc 4
nop 1
crap 4
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