Completed
Pull Request — master (#127)
by Alexandr
05:03
created

PersonaRequest::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
     * @param PersonaSettings|null $personaSettings
23
     */
24 7
    public function __construct(string $path, PersonaSettings $personaSettings = null)
25
    {
26 7
        parent::__construct($path);
27
28 7
        $this->personaSettings = $personaSettings;
29 7
    }
30
31
    /**
32
     * @param string $method
33
     *
34
     * @return RequestInterface
35
     */
36 7
    public function build(string $method = 'post'): RequestInterface
37
    {
38 7
        $request = $this->origin->withMethod($method);
39 7
        $body = $this->buildBody();
40 7
        if (!empty($body)) {
41 2
            $request = $request->withBody(stream_for($body));
42
        }
43
44 7
        return $request;
45
    }
46
47
    /**
48
     * @return string
49
     */
50 7
    public function buildBody(): string
51
    {
52 7
        if ($this->personaSettings instanceof PersonaSettings) {
53 2
            return json_encode($this->personaSettings);
54
        }
55
56 5
        return '';
57
    }
58
}
59