Payload::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace InShore\Bookwhen\ValueObjects\Transporter;
6
7
use Http\Discovery\Psr17Factory;
8
use InShore\Bookwhen\Contracts\Request;
0 ignored issues
show
Bug introduced by
The type InShore\Bookwhen\Contracts\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use InShore\Bookwhen\Enums\Transporter\ContentType;
0 ignored issues
show
Bug introduced by
The type InShore\Bookwhen\Enums\Transporter\ContentType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use InShore\Bookwhen\Enums\Transporter\Method;
11
use InShore\Bookwhen\ValueObjects\ResourceUri;
12
use Psr\Http\Message\RequestInterface;
13
14
/**
15
 * @internal
16
 */
17
final class Payload
18
{
19
    /**
20
     * Creates a new Request value object.
21
     *
22
     * @param  array<string, mixed>  $parameters
23
     */
24
    private function __construct(
25
        //private readonly ContentType $contentType,
26 11
        private readonly Method $method,
27
        private readonly ResourceUri $uri,
28
        private readonly array $parameters = [],
29
    ) {
30
        // ..
31
    }
32
33 11
    /**
34
     * Creates a new Payload value object from the given parameters.
35
     */
36
    public static function list(string $resource, array $parameters = []): self
37
    {
38 5
        //var_export($parameters);
39
        //$contentType = ContentType::JSON;
40
        $method = Method::GET;
41
        $uri = ResourceUri::list($resource);
42 5
43 5
        return new self($method, $uri, $parameters);
44
    }
45 5
46
    /**
47
     * Creates a new Payload value object from the given parameters.
48
     */
49
    public static function retrieve(string $resource, string $id, array $parameters = []): self
50
    {
51 6
        // $contentType = ContentType::JSON;
52
        $method = Method::GET;
53
        $uri = ResourceUri::retrieve($resource, $id);
54 6
55 6
        return new self($method, $uri, $parameters);
56
    }
57 6
58
    /**
59
     * Creates a new Psr 7 Request instance.
60
     */
61
    public function toRequest(BaseUri $baseUri, Headers $headers, QueryParams $queryParams): RequestInterface
0 ignored issues
show
Unused Code introduced by
The parameter $queryParams is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

61
    public function toRequest(BaseUri $baseUri, Headers $headers, /** @scrutinizer ignore-unused */ QueryParams $queryParams): RequestInterface

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63 11
        $psr17Factory = new Psr17Factory();
64
65 11
        $uri = $baseUri->toString() . $this->uri->toString();
66
67 11
        if (!empty($this->parameters)) {
68
            $uri .= '?' . http_build_query($this->parameters);
69 11
        }
70 6
71
        $request = $psr17Factory->createRequest($this->method->value, $uri);
72
73 11
        foreach ($headers->toArray() as $name => $value) {
74
            $request = $request->withHeader($name, $value);
75 11
        }
76 11
        return $request;
77
    }
78
}
79