AbstractTimeAwareStage   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 19.18 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 14
loc 73
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasConnected() 0 5 1
A timeoutSetting() 7 7 2
A timeSinceLastIo() 7 7 2
A setSocketOperationTime() 0 18 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Async sockets
4
 *
5
 * @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
namespace AsyncSockets\RequestExecutor\Pipeline;
11
12
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor;
13
use AsyncSockets\RequestExecutor\RequestExecutorInterface;
14
15
/**
16
 * Class AbstractTimeAwareStage
17
 */
18
abstract class AbstractTimeAwareStage extends AbstractStage
19
{
20
    /**
21
     * Return true, if connect time settings should be used for time operations, false otherwise
22
     *
23
     * @param RequestDescriptor $operation Operation object
24
     *
25
     * @return bool
26
     */
27 76
    protected function hasConnected(RequestDescriptor $operation)
28
    {
29 76
        $meta = $operation->getMetadata();
30 76
        return $meta[RequestExecutorInterface::META_CONNECTION_FINISH_TIME] !== null;
31
    }
32
33
    /**
34
     * Return timeout for current socket state
35
     *
36
     * @param RequestDescriptor $operation Operation object
37
     *
38
     * @return double
39
     */
40 76 View Code Duplication
    protected function timeoutSetting(RequestDescriptor $operation)
41
    {
42 76
        $meta = $operation->getMetadata();
43 76
        return !$this->hasConnected($operation) ?
44 76
            $meta[ RequestExecutorInterface::META_CONNECTION_TIMEOUT ] :
45 76
            $meta[ RequestExecutorInterface::META_IO_TIMEOUT ];
46
    }
47
48
    /**
49
     * Return time since last I/O for current socket state
50
     *
51
     * @param RequestDescriptor $operation Operation object
52
     *
53
     * @return double|null
54
     */
55 76 View Code Duplication
    protected function timeSinceLastIo(RequestDescriptor $operation)
56
    {
57 76
        $meta = $operation->getMetadata();
58 76
        return !$this->hasConnected($operation) ?
59 76
            $meta[ RequestExecutorInterface::META_CONNECTION_START_TIME ] :
60 76
            $meta[ RequestExecutorInterface::META_LAST_IO_START_TIME ];
61
    }
62
63
    /**
64
     * Set start or finish time in metadata of the socket
65
     *
66
     * @param RequestDescriptor $requestDescriptor Socket meta data
67
     * @param string            $key Metadata key to set
68
     *
69
     * @return void
70
     * @throws \InvalidArgumentException
71
     */
72 160
    protected function setSocketOperationTime(RequestDescriptor $requestDescriptor, $key)
73
    {
74 160
        $meta  = $requestDescriptor->getMetadata();
75
        $table = [
76 160
            RequestExecutorInterface::META_CONNECTION_START_TIME =>
77 160
                $meta[ RequestExecutorInterface::META_CONNECTION_START_TIME ] === null,
78
79 160
            RequestExecutorInterface::META_CONNECTION_FINISH_TIME =>
80 160
                $meta[RequestExecutorInterface::META_CONNECTION_FINISH_TIME] === null,
81
82 160
            RequestExecutorInterface::META_LAST_IO_START_TIME =>
83 160
                $meta[RequestExecutorInterface::META_CONNECTION_FINISH_TIME] !== null
84 160
        ];
85
86 160
        if (isset($table[$key]) && $table[$key]) {
87 142
            $requestDescriptor->setMetadata($key, microtime(true));
88 142
        }
89 160
    }
90
}
91