FirstLevelRetryFeature   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 81.48%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 0
loc 68
ccs 22
cts 27
cp 0.8148
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A describe() 0 16 1
A setup() 0 21 1
A getMaxRetries() 0 6 2
1
<?php
2
namespace PSB\Core\ErrorHandling\FirstLevelRetry;
3
4
5
use PSB\Core\ErrorHandling\FirstLevelRetry\Pipeline\FirstLevelRetryPipelineStep;
6
use PSB\Core\Feature\Feature;
7
use PSB\Core\KnownSettingsEnum;
8
use PSB\Core\ObjectBuilder\BuilderInterface;
9
use PSB\Core\Pipeline\PipelineModifications;
10
use PSB\Core\Util\Settings;
11
12
class FirstLevelRetryFeature extends Feature
13
{
14
    const DEFAULT_MAX_RETRIES = 5;
15
16
    /**
17
     * Method will always be executed and should be used to determine whether to enable or disable the feature,
18
     * configure default settings, configure dependencies, configure prerequisites and register startup tasks.
19
     */
20 1
    public function describe()
21
    {
22 1
        $this->enableByDefault();
23 1
        $this->registerPrerequisite(
24
            function (Settings $settings) {
25
                return !$settings->tryGet(KnownSettingsEnum::SEND_ONLY);
26 1
            },
27 1
            "Send only endpoints can't use FLR since it only applies to messages being received."
28
        );
29 1
        $this->registerPrerequisite(
30
            function (Settings $settings) {
31
                return $this->getMaxRetries($settings) > 0;
32 1
            },
33 1
            "FLR was disabled in config since it's set to 0."
34
        );
35 1
    }
36
37
    /**
38
     * Method is called if all defined conditions are met and the feature is marked as enabled.
39
     * Use this method to configure and initialize all required components for the feature like
40
     * the steps in the pipeline or the instances/factories in the container.
41
     *
42
     * @param Settings              $settings
43
     * @param BuilderInterface      $builder
44
     * @param PipelineModifications $pipelineModifications
45
     */
46 2
    public function setup(
47
        Settings $settings,
48
        BuilderInterface $builder,
49
        PipelineModifications $pipelineModifications
50
    ) {
51 2
        $maxRetries = $this->getMaxRetries($settings);
52
53 2
        $builder->defineSingleton(FirstLevelRetryStorage::class, new FirstLevelRetryStorage());
54 2
        $builder->defineSingleton(FirstLevelRetryPolicy::class, new FirstLevelRetryPolicy($maxRetries));
55
56 2
        $pipelineModifications->registerStep(
57 2
            'FirstLevelRetryPipelineStep',
58 2
            FirstLevelRetryPipelineStep::class,
59
            function () use ($builder) {
60
                return new FirstLevelRetryPipelineStep(
61
                    $builder->build(FirstLevelRetryStorage::class),
62
                    $builder->build(FirstLevelRetryPolicy::class)
63
                );
64 2
            }
65
        );
66 2
    }
67
68
    /**
69
     * @param Settings $settings
70
     *
71
     * @return int
72
     */
73 2
    private function getMaxRetries(Settings $settings)
74
    {
75 2
        $maxRetries = $settings->tryGet(KnownSettingsEnum::MAX_FLR_RETRIES);
76 2
        $maxRetries = $maxRetries === null ? self::DEFAULT_MAX_RETRIES : $maxRetries;
77 2
        return $maxRetries;
78
    }
79
}
80