Network::__construct()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.9256

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 12
cp 0.6667
rs 9.4222
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 5.9256
1
<?php
2
3
namespace Posprint\Connectors;
4
5
/**
6
 * Class File
7
 * Create a binary file and writes the data line by line.
8
 * And it can also be used to provide direct connections to USB ports.
9
 *
10
 * @category  NFePHP
11
 * @package   Posprint
12
 * @copyright Copyright (c) 2016
13
 * @license   http://www.gnu.org/licenses/lesser.html LGPL v3
14
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
15
 * @link      http://github.com/nfephp-org/posprint for the canonical source repository
16
 */
17
18
use Posprint\Connectors\ConnectorInterface;
19
use Posprint\Connectors\File;
20
use RuntimeException;
21
use InvalidArgumentException;
22
23
class Network extends File implements ConnectorInterface
24
{
25
    /**
26
     * Open a connection to a TCP/IP socket for ethernet printer connections
27
     *
28
     * @param  string $hostname
29
     * @param  int $port
30
     * @throws RuntimeException
31
     */
32 1
    public function __construct($hostname = '', $port = 9100)
33
    {
34 1
        parent::__construct();
35
        if (empty($hostname)) {
36
            throw new InvalidArgumentException("A hostname or a valid IP must be passed.");
37 1
        }
38
        if (empty($port) || ! is_numeric($port)) {
39
            $port = 9100;
40 1
        }
41 1
        $errno = 0;
42 1
        $errstr = '';
43 1
        $this->resource = @fsockopen($hostname, $port, $errno, $errstr);
44 1
        if ($this->resource === false) {
45
            throw new RuntimeException("Cannot initialise NetworkPrintConnector: " . $errstr);
46
        }
47
    }
48
}
49