Completed
Push — master ( 206c33...9e77c2 )
by Oscar
05:44
created

DetectDevice::getDevice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Middleware;
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
    const KEY = 'DEVICE';
16
17
    /**
18
     * Returns the device.
19
     *
20
     * @param ServerRequestInterface $request
21
     *
22
     * @return Mobile_Detect|null
23
     */
24
    public static function getDevice(ServerRequestInterface $request)
25
    {
26
        return Middleware::getAttribute($request, self::KEY);
27
    }
28
29
    /**
30
     * Execute the middleware.
31
     *
32
     * @param ServerRequestInterface $request
33
     * @param ResponseInterface      $response
34
     * @param callable               $next
35
     *
36
     * @return ResponseInterface
37
     */
38
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
39
    {
40
        $device = new Mobile_Detect($request->getServerParams());
41
42
        return $next(Middleware::setAttribute($request, self::KEY, $device), $response);
43
    }
44
}
45