DetectDevice   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 3
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDevice() 0 4 1
A __invoke() 0 6 1
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Mobile_Detect;
9
10
/**
11
 * Middleware to detect devices using Mobile_Detect library.
12
 */
13
class DetectDevice
14
{
15
    use Utils\AttributeTrait;
16
17
    const KEY = 'DEVICE';
18
19
    /**
20
     * Returns the device.
21
     *
22
     * @param ServerRequestInterface $request
23
     *
24
     * @return Mobile_Detect|null
25
     */
26
    public static function getDevice(ServerRequestInterface $request)
27
    {
28
        return self::getAttribute($request, self::KEY);
29
    }
30
31
    /**
32
     * Execute the middleware.
33
     *
34
     * @param ServerRequestInterface $request
35
     * @param ResponseInterface      $response
36
     * @param callable               $next
37
     *
38
     * @return ResponseInterface
39
     */
40
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
41
    {
42
        $device = new Mobile_Detect($request->getServerParams());
43
44
        return $next(self::setAttribute($request, self::KEY, $device), $response);
45
    }
46
}
47