Passed
Push — develop ( d07d22...02265f )
by Manuele
14:21
created

ClassicVotifier::preparePackage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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\ServerType;
14
15
use D3strukt0r\VotifierClient\Exception\NotVotifierException;
16
use D3strukt0r\VotifierClient\Exception\PackageNotSentException;
17
use D3strukt0r\VotifierClient\ServerConnection;
18
use D3strukt0r\VotifierClient\VoteType\VoteInterface;
19
20
/**
21
 * The Class to access a server which uses the classic "Votifier" plugin.
22
 */
23
class ClassicVotifier extends GenericServerType
24
{
25
    /**
26
     * {@inheritdoc}
27
     *
28
     * @throws NotVotifierException    If the connection response receive didn't start with VOTIFIER...
29
     * @throws PackageNotSentException When the package couldn't be sent
30
     */
31
    public function send(ServerConnection $connection, VoteInterface $vote): void
32
    {
33
        if (!$this->verifyConnection($connection->receive(64))) {
34
            throw new NotVotifierException();
35
        }
36
37
        if (!$connection->send($this->preparePackage($vote))) {
38
            throw new PackageNotSentException();
39
        }
40
    }
41
42
    /**
43
     * Verifies that the connection is correct.
44
     *
45
     * @param string|null $header (Required) The header that the plugin usually sends
46
     *
47 3
     * @return bool returns true if connections is available, otherwise false
48
     */
49 3
    private function verifyConnection(?string $header): bool
50
    {
51 3
        if (null === $header || false === mb_strpos($header, 'VOTIFIER')) {
52 3
            return false;
53
        }
54
55 3
        return true;
56 3
    }
57 3
58 3
    /**
59 3
     * Create encrypted package for default Votifier.
60
     *
61
     * @param voteInterface $vote (Required) The vote package with all the information
62 3
     *
63
     * @return string returns the string to be sent to the server
64
     */
65
    private function preparePackage(VoteInterface $vote): string
66
    {
67 1
        // Details of the vote
68
        $votePackage = 'VOTE' . "\n" .
69 1
            $vote->getServiceName() . "\n" .
70
            $vote->getUsername() . "\n" .
71
            $vote->getAddress() . "\n" .
72
            $vote->getTimestamp() . "\n";
73
74
        // Encrypt the string
75 1
        openssl_public_encrypt($votePackage, $encryptedVotePackage, $this->getPublicKey());
76
77 1
        return $encryptedVotePackage;
78
    }
79
}
80