Completed
Pull Request — master (#3)
by Brendan
02:30
created

Client::setReadTimeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Graze\WipotecCheckweigherClient;
4
5
use Graze\WipotecCheckweigherClient\Request\RequestInterface;
6
use Graze\WipotecCheckweigherClient\Response\ResponseGeneric;
7
use Graze\TelnetClient\InterpretAsCommand;
8
use Graze\TelnetClient\TelnetClient;
9
use Graze\TelnetClient\TelnetClientInterface;
10
use Socket\Raw\Factory as SocketFactory;
11
12
class Client implements ClientInterface
13
{
14
    // The checkweigher telnet interface doesn't actually have a prompt however we need an identifier to distinguish
15
    // error responses, this is explicitly passed instead of using the default error prompt in case it changes.
16
    const PROMPT_ERROR = 'ERROR';
17
18
    /** @var TelnetClientInterface */
19
    protected $telnetClient;
20
21
    /**
22
     * @param TelnetClientInterface $telnetClient
23
     */
24 4
    public function __construct(TelnetClientInterface $telnetClient)
25
    {
26 4
        $this->telnetClient = $telnetClient;
27 4
    }
28
29
    /**
30
     * @param string $dsn
31
     * @param float|null $timeout
32
     */
33 1
    public function connect($dsn, $timeout = null)
34
    {
35 1
        $this->telnetClient->connect($dsn, null, self::PROMPT_ERROR, '', $timeout);
0 ignored issues
show
Unused Code introduced by
The call to Graze\TelnetClient\Telne...entInterface::connect() has too many arguments starting with $timeout. ( Ignorable by Annotation )

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

35
        $this->telnetClient->/** @scrutinizer ignore-call */ 
36
                             connect($dsn, null, self::PROMPT_ERROR, '', $timeout);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
36 1
    }
37
38
    /**
39
     * @param float $timeout
40
     */
41
    public function setReadTimeout($timeout)
42
    {
43
        $this->telnetClient->setReadTimeout($timeout);
44
    }
45
46
    /**
47
     * @param RequestInterface $request
48
     */
49 2
    public function sendRequest(RequestInterface $request)
50
    {
51 2
        $telnetResponse = $this->telnetClient->execute($request->getXml());
52
53
        // Use a specific response class if it exists.
54
        // e.g. with Request/RequestReadStatus try to use Response/ResponseReadStatus.
55 2
        $responseClassName = str_replace('Request', 'Response', get_class($request));
56 2
        if (!class_exists($responseClassName)) {
57 1
            $responseClassName = ResponseGeneric::class;
58
        }
59
60 2
        $response = new $responseClassName($telnetResponse);
61
62 2
        return $response;
63
    }
64
65
    /**
66
     * @return Client
67
     */
68 1
    public static function factory()
69
    {
70 1
        $telnetClient = new TelnetClient(
71 1
            new SocketFactory(),
72 1
            new TelnetPromptMatcher(self::PROMPT_ERROR),
73 1
            new InterpretAsCommand()
74
        );
75
76 1
        return new static($telnetClient);
77
    }
78
}
79