Passed
Push — develop ( fa4bea...483a4e )
by Manuele
68:47
created

Votifier::verifyConnectionHeader()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
/**
4
 * Votifier PHP Client
5
 *
6
 * @package   VotifierClient
7
 * @author    Manuele Vaccari <[email protected]>
8
 * @copyright Copyright (c) 2017-2020 Manuele Vaccari <[email protected]>
9
 * @license   https://github.com/D3strukt0r/votifier-client-php/blob/master/LICENSE.txt GNU General Public License v3.0
10
 * @link      https://github.com/D3strukt0r/votifier-client-php
11
 */
12
13
namespace D3strukt0r\Votifier\Client\Server;
14
15
use D3strukt0r\Votifier\Client\Exception\NotVotifierException;
16
use D3strukt0r\Votifier\Client\Vote\VoteInterface;
17
use DateTime;
18
use InvalidArgumentException;
19
20
/**
21
 * The Class to access a server which uses the classic "Votifier" plugin.
22
 */
23
class Votifier extends GenericServer
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 2
    public function verifyConnection(): void
29
    {
30
        // Check if all variables have been set, to create a connection
31 2
        $this->checkVariablesForSocket();
32
33
        // Connect to the server
34 2
        $socket = $this->getSocket();
35 2
        $socket->open($this->getHost(), $this->getPort());
36
37
        // Check whether the connection really belongs to a Votifier plugin
38 2
        if (!$this->verifyConnectionHeader($socket->read(64))) {
39 1
            throw new NotVotifierException();
40
        }
41 1
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 13
    public function sendVote(VoteInterface ...$votes): void
47
    {
48
        // Check if all variables have been set, to create a connection
49 13
        $this->checkVariablesForSocket();
50
51 11
        foreach ($votes as $vote) {
52
            // Connect to the server
53 11
            $socket = $this->getSocket();
54 11
            $socket->open($this->getHost(), $this->getPort());
55
56
            // Check whether the connection really belongs to a Votifier plugin
57 10
            if (!$this->verifyConnectionHeader($socket->read(64))) {
58 1
                throw new NotVotifierException();
59
            }
60
61
            // Update the timestamp of the vote being sent
62 9
            $vote->setTimestamp(new DateTime());
63
64
            // Check if all variables have been set, to create a package
65 9
            $this->checkVariablesForPackage($vote);
66
67
            // Send the vote
68 2
            $socket->write($this->preparePackage($vote));
69
70
            // Make sure to close the connection after package was sent
71 1
            $socket->__destruct();
72
        }
73 1
    }
74
75
    /**
76
     * Check that both host and port have been set.
77
     *
78
     * @throws InvalidArgumentException If one required parameter wasn't set
79
     */
80 15
    protected function checkVariablesForSocket(): void
81
    {
82 15
        if (!isset($this->host, $this->port)) {
83
            // $countError = 0;
84 2
            $errorMessage = '';
85
86 2
            if (null === $this->host) {
87 2
                $errorMessage .= 'The host variable wasn\'t set with "->setHost(...)".';
88
                // ++$countError;
89
            }
90
            // Not needed, as port has a default value
91
            // if (null === $this->port) {
92
            //     $errorMessage .= $countError > 0 ? ' ' : '';
93
            //     $errorMessage .= 'The port variable wasn\'t set with "->setPort(...)".';
94
            // }
95
96 2
            throw new InvalidArgumentException($errorMessage);
97
        }
98 13
    }
99
100
    /**
101
     * Check that service name, username, address, timestamp and public key have been set.
102
     *
103
     * @param VoteInterface $vote The vote to check
104
     *
105
     * @throws InvalidArgumentException If one required parameter wasn't set
106
     */
107 9
    protected function checkVariablesForPackage(VoteInterface $vote)
108
    {
109
        if (
110 9
            null === $vote->getServiceName()
111 4
            || null === $vote->getUsername()
112 3
            || null === $vote->getAddress()
113 2
            || null === $vote->getTimestamp()
114 9
            || !isset($this->publicKey)
115
        ) {
116 7
            $countError = 0;
117 7
            $errorMessage = '';
118
119 7
            if (null === $vote->getServiceName()) {
120 5
                $errorMessage .= 'The host variable wasn\'t set with "->setServiceName(...)".';
121 5
                ++$countError;
122
            }
123 7
            if (null === $vote->getUsername()) {
124 5
                $errorMessage .= $countError > 0 ? ' ' : '';
125 5
                $errorMessage .= 'The host variable wasn\'t set with "->setUsername(...)".';
126 5
                ++$countError;
127
            }
128 7
            if (null === $vote->getAddress()) {
129 6
                $errorMessage .= $countError > 0 ? ' ' : '';
130 6
                $errorMessage .= 'The host variable wasn\'t set with "->setAddress(...)".';
131 6
                ++$countError;
132
            }
133 7
            if (null === $vote->getTimestamp()) {
134 6
                $errorMessage .= $countError > 0 ? ' ' : '';
135 6
                $errorMessage .= 'The host variable wasn\'t set with "->setTimestamp(...)".';
136
            }
137 7
            if (!isset($this->publicKey)) {
138 6
                $errorMessage .= $countError > 0 ? ' ' : '';
139 6
                $errorMessage .= 'The public key variable wasn\'t set with "->setPublicKey(...)".';
140
            }
141
142 7
            throw new InvalidArgumentException($errorMessage);
143
        }
144 2
    }
145
146
    /**
147
     * Verifies that the connection is correct. Read more:
148
     * https://github.com/vexsoftware/votifier/wiki/Protocol-Documentation.
149
     *
150
     * @param string|null $header The header that the plugin usually sends
151
     *
152
     * @return bool returns true if connections is available, otherwise false
153
     */
154 12
    protected function verifyConnectionHeader(?string $header): bool
155
    {
156
        if (
157 12
            null === $header
158 12
            || false === mb_strpos($header, 'VOTIFIER')
159
        ) {
160 2
            return false;
161
        }
162
163 10
        return true;
164
    }
165
166
    /**
167
     * Create encrypted package for default Votifier. Read more:
168
     * https://github.com/vexsoftware/votifier/wiki/Protocol-Documentation.
169
     *
170
     * @param VoteInterface $vote The vote package with all the information
171
     *
172
     * @return string returns the string to be sent to the server
173
     */
174 2
    protected function preparePackage(VoteInterface $vote): string
175
    {
176
        // Details of the vote
177
        $votePackage = 'VOTE' . "\n" .
178 2
            $vote->getServiceName() . "\n" .
179 2
            $vote->getUsername() . "\n" .
180 2
            $vote->getAddress() . "\n" .
181 2
            $vote->getTimestamp() . "\n";
182
183
        // Encrypt the string
184 2
        openssl_public_encrypt($votePackage, $encryptedVotePackage, $this->getPublicKey(), OPENSSL_SSLV23_PADDING);
185
186 2
        return $encryptedVotePackage;
187
    }
188
}
189