Loop::execute()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 24
rs 9.4888
cc 5
nc 5
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\AppBundle\Workflow\Vacancy\Worker\Iteration;
17
18
use Closure;
19
use Exception;
20
use Psr\Log\LoggerInterface;
21
use Veslo\AppBundle\Workflow\Vacancy\PitInterface;
22
use Veslo\AppBundle\Workflow\Vacancy\WorkerInterface;
23
24
/**
25
 * Represent common execution logic for a set of similar iterations
26
 */
27
class Loop
28
{
29
    /**
30
     * An executor of iteration
31
     *
32
     * @var WorkerInterface
33
     */
34
    private $worker;
35
36
    /**
37
     * A business logic to be executed multiple times
38
     *
39
     * @var Closure
40
     */
41
    private $iteration;
42
43
    /**
44
     * Message for an unsuccessful execution
45
     *
46
     * @var string
47
     */
48
    private $errorMessage;
49
50
    /**
51
     * Loop constructor.
52
     *
53
     * @param WorkerInterface $worker       An executor of iteration
54
     * @param Closure         $iteration    A business logic to be executed multiple times
55
     * @param string          $errorMessage Message for an unsuccessful execution
56
     */
57
    public function __construct(
58
        WorkerInterface $worker,
59
        Closure $iteration,
60
        string $errorMessage
61
    ) {
62
        $this->worker       = $worker;
63
        $this->iteration    = $iteration;
64
        $this->errorMessage = $errorMessage;
65
    }
66
67
    /**
68
     * Performs an iteration loop for executor's business logic
69
     *
70
     * @param PitInterface $source     Data source
71
     * @param int          $iterations Iterations count
72
     *
73
     * @return int Successful iterations count
74
     */
75
    public function execute(PitInterface $source, int $iterations): int
76
    {
77
        $iterationRemains = max(1, $iterations);
78
        $iterationSuccess = 0;
79
        $iterationClosure = $this->iteration;
80
81
        while ($iterationRemains > 0) {
82
            try {
83
                if ($iterationClosure($source)) {
84
                    ++$iterationSuccess;
85
                }
86
            } catch (Exception $e) {
87
                $logger = $this->worker->getLogger();
88
89
                if ($logger instanceof LoggerInterface) {
90
                    $context = ['source' => get_class($source), 'message' => $e->getMessage()];
91
                    $logger->error($this->errorMessage, $context);
92
                }
93
            }
94
95
            --$iterationRemains;
96
        }
97
98
        return $iterationSuccess;
99
    }
100
}
101