Issues (283)

src/UnicontrollerClient.php (1 issue)

1
<?php
2
/**
3
 * This file is part of graze/unicontroller-client.
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/unicontroller-client/blob/master/LICENSE.md
11
 * @link https://github.com/graze/unicontroller-client
12
 */
13
namespace Graze\UnicontrollerClient;
14
15
use Socket\Raw\Factory as SocketFactory;
16
use Graze\UnicontrollerClient\CommandSerialiser;
17
use Graze\UnicontrollerClient\Parser\ParserResolver;
18
use Graze\UnicontrollerClient\SocketReader;
19
use Graze\UnicontrollerClient\Entity\Entity\EntityInterface;
20
use Graze\UnicontrollerClient\Serialiser\SerialiserResolver;
21
22
class UnicontrollerClient
23
{
24
    /**
25
     * @var SocketFactory
26
     */
27
    private $socketFactory;
28
29
    /**
30
     * @var Serialiser
31
     */
32
    private $commandSerialiser;
33
34
    /**
35
     * @var ParserResolver
36
     */
37
    private $parserResolver;
38
39
    /**
40
     * @var SocketReader
41
     */
42
    private $socketReader;
43
44
    /**
45
     * @var SerialiserResolver
46
     */
47
    private $serialiserResolver;
48
49
    /**
50
     * @var \Socket\Raw\Socket
51
     */
52
    private $socket;
53
54
    /**
55
     * @param SocketFactory $socketFactory
56
     * @param CommandSerialiser $commandSerialiser
57
     * @param ParserResolver $parserResolver
58
     * @param SocketReader $socketReader
59
     * @param SerialiserResolver $serialiserResolver
60
     */
61 19
    public function __construct(
62
        SocketFactory $socketFactory,
63
        CommandSerialiser $commandSerialiser,
64
        ParserResolver $parserResolver,
65
        SocketReader $socketReader,
66
        SerialiserResolver $serialiserResolver
67
    ) {
68 19
        $this->socketFactory = $socketFactory;
69 19
        $this->commandSerialiser = $commandSerialiser;
0 ignored issues
show
Documentation Bug introduced by
It seems like $commandSerialiser of type Graze\UnicontrollerClient\CommandSerialiser is incompatible with the declared type Graze\UnicontrollerClient\Serialiser of property $commandSerialiser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70 19
        $this->parserResolver = $parserResolver;
71 19
        $this->socketReader = $socketReader;
72 19
        $this->serialiserResolver = $serialiserResolver;
73 19
    }
74
75
    /**
76
     * @param string $dsn
77
     */
78
    public function connect($dsn)
79
    {
80
        $this->socket = $this->socketFactory->createClient($dsn);
81
    }
82
83
    /**
84
     * @param string $command
85
     * @param array $arguments
86
     * @return EntityInterface
87
     */
88
    public function __call($command, array $arguments)
89
    {
90
        $argumentsSerialised = $this->commandSerialiser->serialiseArguments($arguments);
91
        return $this->send($command, $argumentsSerialised);
92
    }
93
94
    /**
95
     * @param string $command
96
     * @param string $argumentsSerialised
97
     * @return EntityInterface
98
     */
99
    public function send($command, $argumentsSerialised)
100
    {
101
        $commandSerialised = $this->commandSerialiser->serialiseCommand($command, $argumentsSerialised);
102
        $this->socket->write($commandSerialised);
103
        $response = $this->socketReader->read($this->socket);
104
105
        $parser = $this->parserResolver->resolve($command);
106
        return $parser->parse($response);
107
    }
108
109
    /**
110
     * @param EntityInterface $entity
111
     * @return string
112
     */
113 19
    public function serialise(EntityInterface $entity)
114
    {
115 19
        $serialiser = $this->serialiserResolver->resolve($entity);
116 19
        return $serialiser->serialise($entity);
117
    }
118
119
    /**
120
     * @return UnicontrollerClient
121
     */
122 19
    public static function factory()
123
    {
124 19
        return new static(
125 19
            new SocketFactory(),
126 19
            CommandSerialiser::factory(),
127 19
            new ParserResolver(),
128 19
            new SocketReader(),
129 19
            new SerialiserResolver()
130 19
        );
131
    }
132
}
133