HttpResponseBuilder::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of Phiremock.
4
 *
5
 * Phiremock is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * Phiremock is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with Phiremock.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace Mcustiel\Phiremock\Client\Utils;
20
21
use Mcustiel\Phiremock\Domain\Http\BinaryBody;
22
use Mcustiel\Phiremock\Domain\Http\Body;
23
use Mcustiel\Phiremock\Domain\Http\Header;
24
use Mcustiel\Phiremock\Domain\Http\HeaderName;
25
use Mcustiel\Phiremock\Domain\Http\HeadersCollection;
26
use Mcustiel\Phiremock\Domain\Http\HeaderValue;
27
use Mcustiel\Phiremock\Domain\Http\StatusCode;
28
use Mcustiel\Phiremock\Domain\HttpResponse;
29
use Mcustiel\Phiremock\Domain\Response;
30
31
class HttpResponseBuilder extends ResponseBuilder
32
{
33
    /** @var StatusCode */
34
    private $statusCode;
35
36
    /** @var Body */
37
    private $body;
38
39
    /** @var HeadersCollection */
40
    private $headers;
41
42
    public function __construct(StatusCode $statusCode)
43
    {
44
        $this->headers = new HeadersCollection();
45
        $this->statusCode = $statusCode;
46
        $this->body = Body::createEmpty();
47
    }
48
49
    public static function create(int $statusCode): HttpResponseBuilder
50
    {
51
        return new static(new StatusCode($statusCode));
52
    }
53
54
    public function andBody(string $body): HttpResponseBuilder
55
    {
56
        $this->body = new Body($body);
57
58
        return $this;
59
    }
60
61
    public function andBinaryBody(string $body): HttpResponseBuilder
62
    {
63
        $this->body = new BinaryBody($body);
64
65
        return $this;
66
    }
67
68
    public function andHeader(string $header, string $value): HttpResponseBuilder
69
    {
70
        $this->headers->setHeader(
71
            new Header(new HeaderName($header), new HeaderValue($value))
72
        );
73
74
        return $this;
75
    }
76
77
    /** @return Response|HttpResponse */
78
    public function build(): Response
79
    {
80
        $response = parent::build();
81
82
        return new HttpResponse(
83
            $this->statusCode,
84
            $this->body,
85
            $this->headers,
86
            $response->getDelayMillis(),
87
            $response->getNewScenarioState()
88
        );
89
    }
90
}
91