Completed
Pull Request — master (#1)
by Raymond
02:20
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace RayRutjes\GetEventStore\Client\Http;
4
5
use GuzzleHttp\Psr7\Request;
6
use Psr\Http\Message\RequestInterface;
7
8
final class ReadEventStreamViaPersistentSubscriptionRequestFactory implements RequestFactoryInterface
9
{
10
    /**
11
     * @var int
12
     */
13
    private $batchSize;
14
15
    /**
16
     * @var string
17
     */
18
    private $uri;
19
20
    /**
21
     * @param string   $uri
22
     * @param int|null $batchSize
23
     */
24
    public function __construct(string $uri, int $batchSize = 1)
25
    {
26
        $this->uri = $uri;
27
        $this->batchSize = $batchSize;
28
    }
29
30
    /**
31
     * @return RequestInterface
32
     */
33
    public function buildRequest(): RequestInterface
34
    {
35
        $batchSizePart = '/' . (string) $this->batchSize;
36
37
        $path = $this->uri;
38
        // if path does not already ends with the batchSize part, add it.
39
        if (($temp = strlen($path) - strlen($batchSizePart)) < 0 || strpos($path, $batchSizePart, $temp) === false) {
40
            $path .= $batchSizePart;
41
        }
42
        $path .= '?' . ReadEventStreamFeedRequestFactory::EMBED . '=' . ReadEventStreamFeedRequestFactory::EMBED_BODY;
43
44
        return new Request(
45
            'GET',
46
            $path,
47
            [
48
                RequestHeader::ACCEPT => ContentType::COMPETING_ATOM_JSON,
49
            ]
50
        );
51
    }
52
}
53