Completed
Pull Request — master (#127)
by Alexandr
04:00
created

PersonaRequest::buildHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Request;
6
7
use Kerox\Messenger\Model\PersonaSettings;
8
use Psr\Http\Message\RequestInterface;
9
use function GuzzleHttp\Psr7\stream_for;
10
11
class PersonaRequest extends AbstractRequest implements BodyRequestInterface
12
{
13
    /**
14
     * @var PersonaSettings|null
15
     */
16
    protected $personaSettings;
17
18
    /**
19
     * ProfileRequest constructor.
20
     *
21
     * @param string               $path
22 4
     * @param PersonaSettings|null $personaSettings
23
     */
24 4
    public function __construct(string $path, PersonaSettings $personaSettings = null)
25
    {
26 4
        parent::__construct($path);
27 4
28
        $this->personaSettings = $personaSettings;
29
    }
30
31
    /**
32 4
     * @param string|null $method
33
     *
34
     * @return RequestInterface
35 4
     */
36
    public function build(?string $method = null): RequestInterface
37
    {
38 4
        $request = $this->origin->withMethod($method);
39
        $body = $this->buildBody();
40
        if (!empty($body)) {
41
            $request = $request->withBody(stream_for($this->buildBody()));
42
        }
43
44 4
        return $request;
45
    }
46 4
47 1
    /**
48
     * @return string
49 3
     */
50
    public function buildBody(): string
51
    {
52
        if ($this->personaSettings instanceof PersonaSettings) {
53
            return json_encode($this->personaSettings);
54
        }
55
56
        return '';
57
    }
58
}
59