AbstractStage   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 114
Duplicated Lines 29.82 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 34
loc 114
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A callSocketSubscribers() 16 16 2
A callExceptionSubscribers() 18 18 2
A createEvent() 0 11 1

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\Event\Event;
13
use AsyncSockets\Exception\SocketException;
14
use AsyncSockets\RequestExecutor\ExecutionContext;
15
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor;
16
use AsyncSockets\RequestExecutor\RequestExecutorInterface;
17
18
/**
19
 * Class AbstractStage
20
 */
21
abstract class AbstractStage implements PipelineStageInterface
22
{
23
    /**
24
     * Event caller
25
     *
26
     * @var EventCaller
27
     */
28
    protected $eventCaller;
29
30
    /**
31
     * Request executor
32
     *
33
     * @var RequestExecutorInterface
34
     */
35
    protected $executor;
36
37
    /**
38
     * Execution context
39
     *
40
     * @var ExecutionContext
41
     */
42
    protected $executionContext;
43
44
    /**
45
     * AbstractStage constructor.
46
     *
47
     * @param RequestExecutorInterface $executor         Request executor
48
     * @param EventCaller              $eventCaller      Event caller
49
     * @param ExecutionContext         $executionContext Execution context
50
     */
51 204
    public function __construct(
52
        RequestExecutorInterface $executor,
53
        EventCaller $eventCaller,
54
        ExecutionContext $executionContext
55
    ) {
56 204
        $this->executor         = $executor;
57 204
        $this->eventCaller      = $eventCaller;
58 204
        $this->executionContext = $executionContext;
59 204
    }
60
61
    /**
62
     * Notify handlers about given event
63
     *
64
     * @param RequestDescriptor $requestDescriptor Request descriptor
65
     * @param Event             $event Event object
66
     *
67
     * @return void
68
     * @throws \Exception
69
     */
70 160 View Code Duplication
    protected function callSocketSubscribers(RequestDescriptor $requestDescriptor, Event $event)
71
    {
72
        try {
73 160
            $this->eventCaller->setCurrentOperation($requestDescriptor);
74 160
            $this->eventCaller->callSocketSubscribers(
75 160
                $requestDescriptor,
76 160
                $event,
77 160
                $this->executor,
78 160
                $this->executionContext
79 160
            );
80 146
            $this->eventCaller->clearCurrentOperation();
81 160
        } catch (\Exception $e) {
82 54
            $this->eventCaller->clearCurrentOperation();
83 54
            throw $e;
84
        }
85 146
    }
86
87
    /**
88
     * Notify handlers about exception
89
     *
90
     * @param RequestDescriptor $requestDescriptor Socket operation object
91
     * @param SocketException   $exception Thrown exception
92
     *
93
     * @return void
94
     * @throws \Exception
95
     */
96 47 View Code Duplication
    protected function callExceptionSubscribers(
97
        RequestDescriptor $requestDescriptor,
98
        SocketException $exception
99
    ) {
100
        try {
101 47
            $this->eventCaller->setCurrentOperation($requestDescriptor);
102 47
            $this->eventCaller->callExceptionSubscribers(
103 47
                $requestDescriptor,
104 47
                $exception,
105 47
                $this->executor,
106 47
                $this->executionContext
107 47
            );
108 43
            $this->eventCaller->clearCurrentOperation();
109 47
        } catch (\Exception $e) {
110 4
            $this->eventCaller->clearCurrentOperation();
111 4
            throw $e;
112
        }
113 43
    }
114
115
    /**
116
     * Create simple event
117
     *
118
     * @param RequestDescriptor $operation Operation item
119
     * @param string            $eventName Event name for object
120
     *
121
     * @return Event
122
     */
123 150
    protected function createEvent(RequestDescriptor $operation, $eventName)
124
    {
125 150
        $meta = $operation->getMetadata();
126
127 150
        return new Event(
128 150
            $this->executor,
129 150
            $operation->getSocket(),
130 150
            $meta[ RequestExecutorInterface::META_USER_CONTEXT ],
131
            $eventName
132 150
        );
133
    }
134
}
135