ConcurrentProofFunctionCaller::executeFunction()   B
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 10
cts 10
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 12
nc 3
nop 3
crap 4
1
<?php
2
/******************************************************************************
3
 * Copyright (c) 2016 Constantin Galbenu <[email protected]>             *
4
 ******************************************************************************/
5
6
namespace Gica\Cqrs\Command\CommandDispatcher;
7
8
9
use Gica\Cqrs\Command\Exception\TooManyCommandExecutionRetries;
10
use Gica\Cqrs\EventStore\Exception\ConcurrentModificationException;
11
12
class ConcurrentProofFunctionCaller
13
{
14 8
    public function executeFunction($pureFunction, $maxRetries, array $arguments = [])
15
    {
16 8
        $retries = -1;
17
        do {
18
            try {
19
20
                /**
21
                 * The real function call
22
                 */
23 8
                return call_user_func_array($pureFunction, $arguments);
24
25 3
            } catch (ConcurrentModificationException $e) {
26
27 2
                $retries++;
28 2
                if ($retries >= $maxRetries) {
29 1
                    break;
30
                }
31
32 2
                continue;//retry
33
            }
34
35 2
        } while (true);
36
37 1
        throw new TooManyCommandExecutionRetries(sprintf("TooManyCommandExecutionRetries: %d (%s)", $retries, $e->getMessage()));
38
    }
39
}