MobileDetectFactory::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
3
namespace MobileDetectModule\Service;
4
5
use Detection\MobileDetect;
6
use Interop\Container\ContainerInterface;
7
use Zend\Http\PhpEnvironment\Request as HttpRequest;
8
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
9
10
/**
11
 * Creates and builds MobileDetect service.
12
 *
13
 * @author Nikola Posa <[email protected]>
14
 */
15
class MobileDetectFactory
16
{
17
    /**
18
     * @param ContainerInterface $container
19
     * @return MobileDetect
20
     */
21 2
    public function __invoke(ContainerInterface $container)
22
    {
23 2
        if (! $container->has('Request')) {
24 1
            throw new ServiceNotCreatedException('Request object required for creating MobileDetect was not found in the container');
25
        }
26
27
        /* @var $request HttpRequest */
28 1
        $request = $container->get('Request');
29
30 1
        return new MobileDetect(
31 1
            $request->getServer()->toArray(),
32 1
            $request->getServer('HTTP_USER_AGENT')
33
        );
34
    }
35
}
36