ConstantLimitationSolver::invokeEvent()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 4
crap 3
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;
11
12
use AsyncSockets\Event\Event;
13
use AsyncSockets\Event\EventType;
14
use AsyncSockets\Socket\SocketInterface;
15
16
/**
17
 * Class ConstantLimitationSolver
18
 */
19
class ConstantLimitationSolver implements LimitationSolverInterface, EventHandlerInterface
20
{
21
    /**
22
     * Limit of running requests
23
     *
24
     * @var int
25
     */
26
    private $limit;
27
28
    /**
29
     * Key with number of active requests in execution context
30
     *
31
     * @var string
32
     */
33
    private $key;
34
35
    /**
36
     * ConstantLimitationSolver constructor.
37
     *
38
     * @param int $limit Limit of running requests
39
     */
40 6
    public function __construct($limit)
41
    {
42 6
        $this->limit = $limit;
43 6
        $this->key   = uniqid(__CLASS__, true);
44 6
    }
45
46
    /** {@inheritdoc} */
47 6
    public function initialize(RequestExecutorInterface $executor, ExecutionContext $executionContext)
48
    {
49 6
        $executionContext->inNamespace($this->key)->set('active_requests', 0);
50 6
    }
51
52
    /** {@inheritdoc} */
53 6
    public function finalize(RequestExecutorInterface $executor, ExecutionContext $executionContext)
54
    {
55
        // empty body
56 6
    }
57
58
    /** {@inheritdoc} */
59 2
    public function decide(
60
        RequestExecutorInterface $executor,
61
        SocketInterface $socket,
62
        ExecutionContext $executionContext,
63
        $totalSockets
64
    ) {
65 2
        $activeRequests = $executionContext->inNamespace($this->key)->get('active_requests');
66 2
        if ($activeRequests + 1 <= $this->limit) {
67 2
            return self::DECISION_OK;
68
        } else {
69 1
            return self::DECISION_PROCESS_SCHEDULED;
70
        }
71
    }
72
73
    /** {@inheritdoc} */
74 2
    public function invokeEvent(
75
        Event $event,
76
        RequestExecutorInterface $executor,
77
        SocketInterface $socket,
78
        ExecutionContext $context
79
    ) {
80 2
        switch ($event->getType()) {
81 2
            case EventType::INITIALIZE:
82 2
                $this->onSocketRequestInitialize($context);
83 2
                break;
84 1
            case EventType::FINALIZE:
85 1
                $this->onSocketRequestFinalize($context);
86 1
                break;
87 2
        }
88 2
    }
89
90
    /**
91
     * Process socket initialize event
92
     *
93
     * @param ExecutionContext $executionContext Execution context
94
     *
95
     * @return void
96
     */
97 2
    private function onSocketRequestInitialize(ExecutionContext $executionContext)
98
    {
99 2
        $context = $executionContext->inNamespace($this->key);
100 2
        $context->set(
101 2
            'active_requests',
102 2
            $context->get('active_requests') + 1
103 2
        );
104 2
    }
105
106
    /**
107
     * Process request termination
108
     *
109
     * @param ExecutionContext $executionContext Execution context
110
     *
111
     * @return void
112
     */
113 1
    private function onSocketRequestFinalize(ExecutionContext $executionContext)
114
    {
115 1
        $context = $executionContext->inNamespace($this->key);
116 1
        $context->set(
117 1
            'active_requests',
118 1
            $context->get('active_requests') - 1
119 1
        );
120 1
    }
121
}
122