Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SocketAbstract
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 create
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 __destruct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 close
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Socket
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Socket;
16
17/**
18 * Socket class.
19 *
20 * @package phpOMS\Socket
21 * @license OMS License 2.0
22 * @link    https://jingga.app
23 * @since   1.0.0
24 */
25abstract class SocketAbstract implements SocketInterface
26{
27    /**
28     * Socket ip.
29     *
30     * @var string
31     * @since 1.0.0
32     */
33    protected $ip = null;
34
35    /**
36     * Socket port.
37     *
38     * @var int
39     * @since 1.0.0
40     */
41    protected $port = null;
42
43    /**
44     * Socket running?
45     *
46     * @var bool
47     * @since 1.0.0
48     */
49    protected $run = true;
50
51    /**
52     * Socket.
53     *
54     * @var null|\Socket
55     * @since 1.0.0
56     */
57    protected $sock;
58
59    /**
60     * {@inheritdoc}
61     */
62    public function create(string $ip, int $port) : void
63    {
64        $this->ip   = $ip;
65        $this->port = $port;
66        $this->sock = \socket_create(\AF_INET, \SOCK_STREAM, \SOL_TCP);
67    }
68
69    /**
70     * Destructor.
71     *
72     * @since 1.0.0
73     */
74    public function __destruct()
75    {
76        $this->close();
77    }
78
79    /**
80     * {@inheritdoc}
81     */
82    public function close() : void
83    {
84        if ($this->sock !== null) {
85            \socket_shutdown($this->sock, 2);
86            \socket_close($this->sock);
87            $this->sock = null;
88        }
89    }
90}