Issues (5)

src/ClientHandler.php (3 issues)

1
<?php
2
3
namespace DMT\WebservicesNl\Client;
4
5
use DMT\WebservicesNl\Client\Request\LoginRequest;
6
use DMT\WebservicesNl\Client\Request\LogoutRequest;
7
use DMT\WebservicesNl\Client\Request\RequestInterface;
8
use DMT\WebservicesNl\Client\Response\LoginResponse;
9
use DMT\WebservicesNl\Client\Response\ResponseInterface;
10
use GuzzleHttp\Client;
0 ignored issues
show
This use statement conflicts with another class in this namespace, DMT\WebservicesNl\Client\Client. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use JMS\Serializer\SerializationContext;
12
use JMS\Serializer\Serializer;
13
14
/**
15
 * Class AbstractHandler
16
 *
17
 * @package DMT\WebservicesNl\Client
18
 */
19
class ClientHandler
20
{
21
    /**
22
     * @var Client
23
     */
24
    protected $httpClient;
25
26
    /**
27
     * @var Serializer
28
     */
29
    protected $serializer;
30
31
    /**
32
     * @var string
33
     */
34
    protected $serializerFormat;
35
36
    /**
37
     * @var string
38
     */
39
    protected $deserializerFormat;
40
41
    /**
42
     * DutchBusinessHandler constructor.
43
     *
44
     * @param Client $httpClient
45
     * @param Serializer $serializer
46
     * @param string $serializerFormat
47
     * @param string|null $deserializerFormat
48
     */
49 3
    public function __construct(
50
        Client $httpClient,
51
        Serializer $serializer,
52
        string $serializerFormat,
53
        string $deserializerFormat = null
54
    ) {
55 3
        $this->httpClient = $httpClient;
56 3
        $this->serializer = $serializer;
57 3
        $this->serializerFormat = $serializerFormat;
58 3
        $this->deserializerFormat = $deserializerFormat ?? $serializerFormat;
59 3
    }
60
61
    /**
62
     * @param LoginRequest|RequestInterface $request
63
     *
64
     * @return LoginResponse|ResponseInterface
65
     */
66 1
    public function login(LoginRequest $request): LoginResponse
67
    {
68 1
        return $this->process($request, LoginResponse::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->process($r...e\LoginResponse::class) returns the type null which is incompatible with the type-hinted return DMT\WebservicesNl\Client\Response\LoginResponse.
Loading history...
69
    }
70
71
    /**
72
     * @param LogoutRequest|RequestInterface $request
73
     *
74
     * @return null
75
     */
76 1
    public function logout(LogoutRequest $request)
77
    {
78 1
        return $this->process($request);
0 ignored issues
show
Are you sure the usage of $this->process($request) targeting DMT\WebservicesNl\Client\ClientHandler::process() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
79
    }
80
81
    /**
82
     * @param RequestInterface $request
83
     * @param string $responseClassName
84
     *
85
     * @return ResponseInterface
86
     */
87 2
    protected function process(RequestInterface $request, string $responseClassName = null): ?ResponseInterface
88
    {
89 2
        $context = SerializationContext::create();
90
91 2
        $request = $this->serializer->serialize($request, $this->serializerFormat, $context);
92
93 2
        $response = $this->httpClient->post('', ['body' => $request]);
94
95 2
        if (!$responseClassName) {
96 1
            return null;
97
        }
98
99 1
        return $this->serializer->deserialize($response->getBody(), $responseClassName, $this->deserializerFormat);
100
    }
101
}
102