Completed
Push — master ( 0df352...724305 )
by John
04:42
created

Dynamark3Client   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 69.69%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 83
ccs 23
cts 33
cp 0.6969
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __call() 0 6 1
A send() 0 22 2
A build() 0 14 1
1
<?php
2
3
/**
4
 * This file is part of graze/dynamark3-client.
5
 *
6
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://github.com/graze/dynamark3-client/blob/master/LICENSE.md
12
 * @link https://github.com/graze/dynamark3-client
13
 */
14
15
namespace Graze\Dynamark3Client;
16
17
use Graze\TelnetClient\TelnetClientInterface;
18
use Graze\Dynamark3Client\CommandResolver;
19
use Graze\Dynamark3Client\Command\CommandInterface;
20
use Graze\TelnetClient\TelnetClient;
21
use Graze\Dynamark3Client\Dynamark3Constants;
22
23
class Dynamark3Client
24
{
25
    /**
26
     * @var TelnetClientInterface
27
     */
28
    protected $telnet;
29
30
    /**
31
     * @var CommandResolver
32
     */
33
    protected $commandResolver;
34
35
    /**
36
     * @param TelnetClientInterface $telnet
37
     * @param CommandResolver $commandResolver
38
     */
39 2
    public function __construct(TelnetClientInterface $telnet, CommandResolver $commandResolver)
40
    {
41 2
        $this->telnet = $telnet;
42 2
        $this->commandResolver = $commandResolver;
43 2
    }
44
45
    /**
46
     * @param string $name
47
     * @param array $arguments
48
     *
49
     * @return Graze\Dynamark3Client\Dynamark3Response;
0 ignored issues
show
Documentation introduced by
The doc-type Graze\Dynamark3Client\Dynamark3Response; could not be parsed: Expected "|" or "end of type", but got ";" at position 39. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
50
     */
51 2
    public function __call($name, array $arguments)
52
    {
53 2
        $command = $this->commandResolver->resolve($name);
54 2
        array_unshift($arguments, $command);
55 2
        return call_user_func_array([$this, 'send'], $arguments);
56
    }
57
58
    /**
59
     * @param CommandInterface $command
60
     *
61
     * @return Graze\Dynamark3Client\Dynamark3Response;
0 ignored issues
show
Documentation introduced by
The doc-type Graze\Dynamark3Client\Dynamark3Response; could not be parsed: Expected "|" or "end of type", but got ";" at position 39. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
62
     */
63 2
    protected function send(CommandInterface $command)
1 ignored issue
show
Unused Code introduced by
The parameter $command is not used and could be removed.

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

Loading history...
64
    {
65 2
        $arguments =  func_get_args();
66 2
        $command = array_shift($arguments);
67
68 2
        $argumentsString = '';
69 2
        if ($arguments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $arguments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
70
            // add quotes to arguments
71 1
            array_walk($arguments, function (&$value) {
72 1
                $value = sprintf('"%s"', $value);
73 1
            });
74
75 1
            $argumentsString = ' ' . implode(' ', $arguments);
76 1
        }
77
78 2
        $telnetResponse = $this->telnet->execute(
79 2
            $command->getCommandText() . $argumentsString,
80 2
            $command->getPrompt()
81 2
        );
82
83 2
        return $command->parseResponse($telnetResponse);
84
    }
85
86
    /**
87
     * @param string $dsn
88
     *
89
     * @return Dynamark3Client
90
     */
91
    public static function build($dsn)
92
    {
93
        $telnetClient = TelnetClient::build(
94
            $dsn,
95
            Dynamark3Constants::PROMPT,
96
            Dynamark3Constants::PROMPT_ERROR,
97
            Dynamark3Constants::LINE_ENDING
98
        );
99
100
        return new static(
101
            $telnetClient,
102
            new CommandResolver()
103
        );
104
    }
105
}
106