| 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; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use D3strukt0r\Votifier\Client\Exception\Socket\NoConnectionException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use D3strukt0r\Votifier\Client\Exception\Socket\PackageNotReceivedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use D3strukt0r\Votifier\Client\Exception\Socket\PackageNotSentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Creates a class for socket functionality. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @codeCoverageIgnore | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | class Socket | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @var resource the connection to the server | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     private $socket; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * Closes the connection when the object is destroyed. | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     public function __destruct() | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         if (is_resource($this->socket)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             fclose($this->socket); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * Creates a socket connection. | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @param string $host The hostname or IP address | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @param int    $port The port of Votifier | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @throws NoConnectionException If connection couldn't be established | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     public function open(string $host, int $port): void | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $socket = @fsockopen($host, $port, $errorNumber, $errorString, 3); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         if (false === $socket) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             throw new NoConnectionException($errorString, $errorNumber); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $this->socket = $socket; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * Sends a string to the server. | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @param string $string The string which should be sent to the server | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * @throws NoConnectionException   If connection has not been set up | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @throws PackageNotSentException If there was an error sending the package | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     public function write(string $string): void | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         if (!is_resource($this->socket)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             throw new NoConnectionException(); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         if (false === fwrite($this->socket, $string)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             throw new PackageNotSentException(); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * Reads a string which is being received from the server. | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * @param int $length [optional] The length of the requested string | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * @throws NoConnectionException       If connection has not been set up | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * @throws PackageNotReceivedException If there was an error receiving the package | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * @return string returns the string received from the server | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public function read(int $length = 64): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         if (!is_resource($this->socket)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             throw new NoConnectionException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if (!$string = fread($this->socket, $length)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             throw new PackageNotReceivedException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  |         return $string; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 99 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 100 |  |  |  |