Completed
Push — master ( f8db25...f08840 )
by Marc
02:09
created

TraceableNetwork::traceAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 3
Ratio 21.43 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 3
loc 14
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 6
1
<?php
2
/*
3
 * This file is part of the phpflo/phpflo-flowtrace package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PhpFlo;
12
13
use PhpFlo\Common\AbstractNetworkAdapter;
14
use PhpFlo\Common\HookableNetworkInterface;
15
use PhpFlo\Common\Network;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PhpFlo\Network.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
use PhpFlo\Common\NetworkAdapterinterface;
17
use PhpFlo\Common\NetworkInterface;
18
use PhpFlo\Exception\FlowException;
19
use PhpFlo\Exception\InvalidDefinitionException;
20
use PhpFlo\Exception\InvalidTypeException;
21
use Psr\Log\LoggerInterface;
22
23
/**
24
 * Network for tracing events.
25
 *
26
 * @package PhpFlo
27
 * @author Marc Aschmann <[email protected]>
28
 */
29
class TraceableNetwork extends AbstractNetworkAdapter implements NetworkAdapterinterface
30
{
31
    const TYPE_DATA = 'data';
32
    const TYPE_CONNECT = 'connect';
33
    const TYPE_DISCONNECT = 'disconnect';
34
    const TYPE_BEGIN_GROUP = 'begin.group';
35
    const TYPE_END_GROUP = 'end.group';
36
37
    /**
38
     * @var array
39
     */
40
    private $actions;
41
42
    /**
43
     * @var LoggerInterface
44
     */
45
    private $logger;
46
47
    /**
48
     * TraceableNetwork constructor.
49
     *
50
     * @param NetworkInterface $network
51
     * @param LoggerInterface $logger
52
     */
53 2
    public function __construct(NetworkInterface $network, LoggerInterface $logger)
54
    {
55 2
        parent::__construct($network);
56
57 2
        $this->actions = [
58 2
            self::TYPE_DATA       => 'DATA',
59 2
            self::TYPE_CONNECT    => 'CONN',
60 2
            self::TYPE_DISCONNECT => 'DISC'
61 2
        ];
62
63 2
        $this->logger = $logger;
64 2
        $this->network->hook(self::TYPE_DATA, 'flowtrace', $this->trace(self::TYPE_DATA));
65 2
        $this->network->hook(self::TYPE_CONNECT, 'flowtrace', $this->trace(self::TYPE_CONNECT));
66 2
        $this->network->hook(self::TYPE_DISCONNECT, 'flowtrace', $this->trace(self::TYPE_DISCONNECT));
67 2
    }
68
69
    /**
70
     * Wrap the creation of the callback
71
     *
72
     * @return \Closure
73
     */
74
    private function trace($type)
75
    {
76 2
        $trace = function () use ($type) {
77
            switch ($type) {
78
                case TraceableNetwork::TYPE_DATA:
79
                    $this->traceData(func_get_args(), $type);
80
                    break;
81
                case TraceableNetwork::TYPE_CONNECT:
82
                case TraceableNetwork::TYPE_DISCONNECT:
83
                    $this->traceAction(func_get_args(), $type);
84
                    break;
85
            }
86 2
        };
87
88 2
        return $trace->bindTo($this);
89
    }
90
91
    /**
92
     * @param array $args
93
     * @param string $type
94
     */
95
    private function traceData(array $args, $type)
96
    {
97
        $data   = $args[0];
98
        $socket = $args[1];
99
100
        if (!is_string($data)) {
101
            $data = serialize($data);
102
        }
103
104
        $to      = $socket->to();
105
        $message = "-> {$to['port']} {$to['process']['id']} ";
106
107
        $from = $socket->from();
108 View Code Duplication
        if (isset($from['process'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
            $message = "{$from['process']['id']} {$from['port']} {$message}";
110
        }
111
112
        $this->logger->info("{$message} {$this->actions[$type]} {$data}");
113
    }
114
115
    /**
116
     * @param array $args
117
     * @param string $type
118
     */
119
    private function traceAction(array $args, $type)
120
    {
121
        $socket = $args[0];
122
123
        $to      = $socket->to();
124
        $message = "-> {$to['port']} {$to['process']['id']} ";
125
126
        $from = $socket->from();
127 View Code Duplication
        if (isset($from['process'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
            $message = "{$from['process']['id']} {$from['port']} {$message}";
129
        }
130
131
        $this->logger->debug("{$message} {$this->actions[$type]}");
132
    }
133
}
134