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

DetectDevice   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 32
rs 10

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\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