Completed
Push — master ( b9d832...421ea6 )
by Phil
06:19 queued 04:14
created

RequestAwareTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 4
c 1
b 1
f 0
lcom 1
cbo 1
dl 0
loc 47
ccs 9
cts 10
cp 0.9
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
getContainer() 0 1 ?
A setRequest() 0 6 1
A getRequest() 0 13 3
1
<?php
2
3
namespace League\Route\Http;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use RuntimeException;
7
8
trait RequestAwareTrait
9
{
10
    /**
11
     * @var \Psr\Http\Message\ServerRequestInterface
12
     */
13
    protected $request;
14
15
    /**
16
     * Set a PSR-7 incoming request implementation.
17
     *
18
     * @param  \Psr\Http\Message\ServerRequestInterface $request
19
     *
20
     * @return $this
21
     */
22 27
    public function setRequest(ServerRequestInterface $request)
23
    {
24 27
        $this->request = $request;
25
26 27
        return $this;
27
    }
28
29
    /**
30
     * Return the request object.
31
     *
32
     * @throws \RuntimeException if a request object cannot be determined
33
     *
34
     * @return \Psr\Http\Message\ServerRequestInterface
35
     */
36 27
    public function getRequest()
37
    {
38 27
        if (! is_null($this->request)) {
39 24
            return $this->request;
40
        }
41
42 3
        if ($this->getContainer()->has('Psr\Http\Message\ServerRequestInterface')) {
43 3
            $this->request = $this->getContainer()->get('Psr\Http\Message\ServerRequestInterface');
44 3
            return $this->request;
45
        }
46
47
        throw new RuntimeException('Unable to determine an incoming request object');
48
    }
49
50
    /**
51
     * @return \Interop\Container\ContainerInterface
52
     */
53
    abstract public function getContainer();
54
}
55