Completed
Push — main ( 439431...5a0a5a )
by Daniel
27s queued 13s
created

Payload::__construct()   A

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 Http\Message\MultipartStream\MultipartStreamBuilder;
0 ignored issues
show
Bug introduced by
The type Http\Message\MultipartSt...\MultipartStreamBuilder 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\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...
10
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...
11
use InShore\Bookwhen\Enums\Transporter\Method;
12
use InShore\Bookwhen\ValueObjects\ResourceUri;
13
use Psr\Http\Message\RequestInterface;
14
use Psr\Http\Message\StreamInterface;
15
16
/**
17
 * @internal
18
 */
19
final class Payload
20
{
21
    /**
22
     * Creates a new Request value object.
23
     *
24
     * @param  array<string, mixed>  $parameters
25
     */
26 11
    private function __construct(
27
        //private readonly ContentType $contentType,
28
        private readonly Method $method,
29
        private readonly ResourceUri $uri,
30
        private readonly array $parameters = [],
31
    ) {
32
        // ..
33 11
    }
34
35
    /**
36
     * Creates a new Payload value object from the given parameters.
37
     */
38 5
    public static function list(string $resource, array $parameters = []): self
39
    {
40
        //var_export($parameters);
41
        //$contentType = ContentType::JSON;
42 5
        $method = Method::GET;
43 5
        $uri = ResourceUri::list($resource);
44
45 5
        return new self($method, $uri, $parameters);
46
    }
47
48
    /**
49
     * Creates a new Payload value object from the given parameters.
50
     */
51 6
    public static function retrieve(string $resource, string $id, array $parameters = []): self
52
    {
53
        // $contentType = ContentType::JSON;
54 6
        $method = Method::GET;
55 6
        $uri = ResourceUri::retrieve($resource, $id);
56
57 6
        return new self($method, $uri, $parameters);
58
    }
59
60
    /**
61
     * Creates a new Psr 7 Request instance.
62
     */
63 11
    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

63
    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...
64
    {
65 11
        $psr17Factory = new Psr17Factory();
66
67 11
        $uri = $baseUri->toString() . $this->uri->toString();
68
69 11
        if (!empty($this->parameters)) {
70 6
            $uri .= '?' . http_build_query($this->parameters);
71
        }
72
73 11
        $request = $psr17Factory->createRequest($this->method->value, $uri);
74
75 11
        foreach ($headers->toArray() as $name => $value) {
76 11
            $request = $request->withHeader($name, $value);
77
        }
78 11
        return $request;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $request could return the type Psr\Http\Message\MessageInterface which includes types incompatible with the type-hinted return Psr\Http\Message\RequestInterface. Consider adding an additional type-check to rule them out.
Loading history...
79
    }
80
}
81