Completed
Push — master ( 7f2c26...da397a )
by
unknown
05:31
created

ResponseModelFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MediaMonks\RestApi\Model;
4
5
use MediaMonks\RestApi\Response\PaginatedResponseInterface;
6
use Symfony\Component\HttpFoundation\Response;
7
8
class ResponseModelFactory
9
{
10
    /**
11
     * @var ResponseModelInterface
12
     */
13
    private $responseModel;
14
15
    /**
16
     * @param ResponseModelInterface $responseModel
17
     */
18 12
    public function __construct(ResponseModelInterface $responseModel)
19
    {
20 12
        $this->responseModel = $responseModel;
21 12
    }
22
23
    /**
24
     * @param mixed $content
25
     * @return ResponseModelInterface
26
     */
27 10
    public function createFromContent($content)
28
    {
29 10
        if ($content instanceof Response) {
30 3
            return $this->createFromResponse($content);
31
        }
32 7
        if ($content instanceof PaginatedResponseInterface) {
33 1
            return $this->createFromPaginatedResponse($content);
34
        }
35 6
        if ($content instanceof \Throwable) {
36 3
            return $this->createFromThrowable($content);
37
        }
38
39 3
        return $this->create()->setData($content);
40
    }
41
42
    /**
43
     * @param Response $response
44
     * @return ResponseModelInterface
45
     */
46 3
    public function createFromResponse(Response $response)
47
    {
48 3
        return $this->create()->setResponse($response);
49
    }
50
51
    /**
52
     * @param PaginatedResponseInterface $response
53
     * @return ResponseModelInterface
54
     */
55 1
    public function createFromPaginatedResponse(PaginatedResponseInterface $response)
56
    {
57 1
        return $this->create()->setPagination($response);
58
    }
59
60
    /**
61
     * @param \Throwable $throwable
62
     *
63
     * @return ResponseModelInterface
64 3
     */
65
    public function createFromThrowable(\Throwable $throwable)
66 3
    {
67
        return $this->create()->setThrowable($throwable);
68
    }
69
70
    /**
71
     * @return ResponseModelInterface
72 10
     */
73
    private function create()
74 10
    {
75
        return clone $this->responseModel;
76
    }
77
}
78