Passed
Push — develop ( 1ccf7a...a64b2b )
by Manuele
01:40
created

Votifier::checkRequiredVariablesForSocket()   A

Complexity

Conditions 6
Paths 17

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.2222
c 0
b 0
f 0
cc 6
nc 17
nop 0
crap 6
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\VotifierClient\Server;
14
15
use D3strukt0r\VotifierClient\Exception\NotVotifierException;
16
use D3strukt0r\VotifierClient\Exception\Socket\NoConnectionException;
17
use D3strukt0r\VotifierClient\Exception\Socket\PackageNotReceivedException;
18
use D3strukt0r\VotifierClient\Exception\Socket\PackageNotSentException;
19
use D3strukt0r\VotifierClient\Vote\VoteInterface;
20
use DateTime;
21
use InvalidArgumentException;
22
23
/**
24
 * The Class to access a server which uses the classic "Votifier" plugin.
25
 */
26
class Votifier extends GenericServerType
27
{
28
    /**
29
     * {@inheritdoc}
30
     *
31
     * @throws InvalidArgumentException    If one required parameter wasn't set
32
     * @throws NoConnectionException       If connection couldn't be established
33
     * @throws NotVotifierException        If the server we are connected to is not a valid Votifier server
34
     * @throws PackageNotReceivedException If there was an error receiving the package
35
     * @throws PackageNotSentException     If there was an error sending the package
36
     */
37 12
    public function sendVote(VoteInterface ...$votes): void
38
    {
39
        // Check if all variables have been set, to create a connection
40 12
        $this->checkRequiredVariablesForSocket();
41
42 10
        foreach ($votes as $vote) {
43
            // Connect to the server
44 10
            $socket = $this->getSocket();
45 10
            $socket->open($this->getHost(), $this->getPort());
46
47
            // Check whether the connection really belongs to a Votifier plugin
48 9
            if (!$this->verifyConnection($socket->read(64))) {
49 1
                throw new NotVotifierException();
50
            }
51
52
            // Update the timestamp of the vote being sent
53 8
            $vote->setTimestamp(new DateTime());
54
55
            // Check if all variables have been set, to create a package
56 8
            $this->checkRequiredVariablesForPackage($vote);
57
58
            // Send the vote
59 2
            $socket->write($this->preparePackage($vote));
60
61
            // Make sure to close the connection after package was sent
62 1
            $socket->__destruct();
63
        }
64 1
    }
65
66
    /**
67
     * @throws InvalidArgumentException If one required parameter wasn't set
68
     */
69 12
    protected function checkRequiredVariablesForSocket(): void
70
    {
71 12
        if (!isset($this->host, $this->port)) {
72 2
            $hostErrorMessage = !isset($this->host) ? 'The host variable wasn\'t set with "->setHost(...)".' : '';
73 2
            $portErrorMessage = !isset($this->post) ? 'The port variable wasn\'t set with "->setPort(...)".' : '';
74 2
            $space = mb_strlen($hostErrorMessage) > 0 && mb_strlen($portErrorMessage) > 0 ? ' ' : '';
75
76 2
            throw new InvalidArgumentException($hostErrorMessage . $space . $portErrorMessage);
77
        }
78 10
    }
79
80
    /**
81
     * @param VoteInterface $vote The vote to check
82
     *
83
     * @throws InvalidArgumentException If one required parameter wasn't set
84
     */
85 8
    protected function checkRequiredVariablesForPackage(VoteInterface $vote)
86
    {
87
        if (
88 8
            null === $vote->getServiceName()
89 4
            || null === $vote->getUsername()
90 3
            || null === $vote->getAddress()
91 8
            || null === $vote->getTimestamp()
92
        ) {
93 6
            $countError = 0;
94 6
            $errorMessage = '';
95
96 6
            if (null === $vote->getServiceName()) {
97 4
                $errorMessage .= 'The host variable wasn\'t set with "->setServiceName(...)".';
98 4
                ++$countError;
99
            }
100 6
            if (null === $vote->getUsername()) {
101 4
                $errorMessage .= $countError > 0 ? ' ' : '';
102 4
                $errorMessage .= 'The host variable wasn\'t set with "->setUsername(...)".';
103 4
                ++$countError;
104
            }
105 6
            if (null === $vote->getAddress()) {
106 5
                $errorMessage .= $countError > 0 ? ' ' : '';
107 5
                $errorMessage .= 'The host variable wasn\'t set with "->setAddress(...)".';
108 5
                ++$countError;
109
            }
110 6
            if (null === $vote->getTimestamp()) {
111 5
                $errorMessage .= $countError > 0 ? ' ' : '';
112 5
                $errorMessage .= 'The host variable wasn\'t set with "->setTimestamp(...)".';
113
            }
114
115 6
            throw new InvalidArgumentException($errorMessage);
116
        }
117 2
    }
118
119
    /**
120
     * Verifies that the connection is correct.. Read more:
121
     * https://github.com/vexsoftware/votifier/wiki/Protocol-Documentation.
122
     *
123
     * @param string|null $header (Required) The header that the plugin usually sends
124
     *
125
     * @return bool returns true if connections is available, otherwise false
126
     */
127 9
    protected function verifyConnection(?string $header): bool
128
    {
129 9
        if (null === $header || false === mb_strpos($header, 'VOTIFIER')) {
130 1
            return false;
131
        }
132
133 8
        return true;
134
    }
135
136
    /**
137
     * Create encrypted package for default Votifier. Read more:
138
     * https://github.com/vexsoftware/votifier/wiki/Protocol-Documentation.
139
     *
140
     * @param voteInterface $vote The vote package with all the information
141
     *
142
     * @return string returns the string to be sent to the server
143
     */
144 2
    protected function preparePackage(VoteInterface $vote): string
145
    {
146
        // Details of the vote
147
        $votePackage = 'VOTE' . "\n" .
148 2
            $vote->getServiceName() . "\n" .
149 2
            $vote->getUsername() . "\n" .
150 2
            $vote->getAddress() . "\n" .
151 2
            $vote->getTimestamp() . "\n";
152
153
        // Encrypt the string
154 2
        openssl_public_encrypt($votePackage, $encryptedVotePackage, $this->getPublicKey(), OPENSSL_SSLV23_PADDING);
155
156 2
        return $encryptedVotePackage;
157
    }
158
}
159