Client   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 88%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 2
b 0
f 0
dl 0
loc 69
ccs 22
cts 25
cp 0.88
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A sendRequest() 0 14 2
A setReadTimeout() 0 3 1
A factory() 0 9 1
A connect() 0 3 1
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
28
        // Set a limit to the number of bytes to read per request, this should hopefully prevent infinite loops if the
29
        // prompt is never returned. 1000 bytes should be plenty.
30 4
        $this->telnetClient->setMaxBytesRead(1000);
31 4
    }
32
33
    /**
34
     * @param string $dsn
35
     * @param float|null $timeout
36
     */
37 1
    public function connect($dsn, $timeout = null)
38
    {
39 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

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