Completed
Push — master ( 43e941...fafc95 )
by Roberto
29:58 queued 15:03
created

Network   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 0
cbo 1
dl 0
loc 25
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 15 5
1
<?php
2
3
namespace Posprint\Connectors;
4
/**
5
 * Class File
6
 * Create a binary file and writes the data line by line.
7
 * And it can also be used to provide direct connections to USB ports.
8
 *
9
 * @category  NFePHP
10
 * @package   Posprint
11
 * @copyright Copyright (c) 2016
12
 * @license   http://www.gnu.org/licenses/lesser.html LGPL v3
13
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
14
 * @link      http://github.com/nfephp-org/posprint for the canonical source repository
15
 */
16
17
use Posprint\Connectors\ConnectorInterface;
18
use Posprint\Connectors\File;
19
use RuntimeException;
20
use InvalidArgumentException;
21
22
class Network extends File implements ConnectorInterface
23
{
24
    /**
25
     * Open a connection to a TCP/IP socket for ethernet printer connections
26
     *
27
     * @param  string $hostname
28
     * @param  int    $port
29
     * @throws RuntimeException
30
     */
31
    public function __construct($hostname = '', $port = 9100) 
32
    {
33
        if (empty($hostname)) {
34
            throw new InvalidArgumentException("A hostname or a valid IP must be passed.");
35
        }
36
        if (empty($port) || ! is_numeric($port)) {
37
            $port = 9100;
38
        }
39
        $errno = 0;
40
        $errstr = '';
41
        $this->resource = @fsockopen($ip, $port, $errno, $errstr);
0 ignored issues
show
Bug introduced by
The variable $ip does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
42
        if($this->resource === false) {
43
            throw new RuntimeException("Cannot initialise NetworkPrintConnector: " . $errstr);
44
        }
45
    }
46
}
47