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

AbstractStrategy   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 3 Features 0
Metric Value
wmc 4
c 7
b 3
f 0
lcom 0
cbo 5
dl 0
loc 38
ccs 11
cts 11
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A determineResponse() 0 19 4
1
<?php
2
3
namespace League\Route\Strategy;
4
5
use Exception;
6
use League\Container\ImmutableContainerAwareInterface;
7
use League\Container\ImmutableContainerAwareTrait;
8
use League\Route\Http\RequestAwareInterface;
9
use League\Route\Http\RequestAwareTrait;
10
use League\Route\Http\ResponseAwareInterface;
11
use League\Route\Http\ResponseAwareTrait;
12
use Psr\Http\Message\ResponseInterface;
13
use RuntimeException;
14
15
abstract class AbstractStrategy implements
16
    ImmutableContainerAwareInterface,
17
    RequestAwareInterface,
18
    ResponseAwareInterface
19
{
20
    use ImmutableContainerAwareTrait;
21
    use RequestAwareTrait;
22
    use ResponseAwareTrait;
23
24
    /**
25
     * Attempt to build a response.
26
     *
27
     * @param  mixed $response
28
     *
29
     * @throws \RuntimeException if a response cannot be built
30
     *
31
     * @return \Psr\Http\Message\ResponseInterface
32
     */
33 21
    protected function determineResponse($response)
34
    {
35 21
        if ($response instanceof ResponseInterface) {
36 12
            return $response;
37
        }
38
39
        try {
40 9
            $body     = $response;
41 9
            $response = $this->getResponse();
42
43 9
            if ($response->getBody()->isWritable()) {
44 9
                $response->getBody()->write($body);
45 6
            }
46 9
        } catch (Exception $e) {
47 3
            throw new RuntimeException('Unable to build a response object from controller return value', 0, $e);
48
        }
49
50 6
        return $response;
51
    }
52
}
53