TransactionAwarePipeline::process()   A
last analyzed

Complexity

Conditions 3
Paths 11

Size

Total Lines 59
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 36
nc 11
nop 1
dl 0
loc 59
rs 9.344
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline;
20
21
use Doctrine\DBAL\Connection;
22
use Exception;
23
use Psr\Log\LoggerInterface;
24
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\AbstractCommand;
25
26
class TransactionAwarePipeline implements Pipeline
27
{
28
    public function __construct(
29
        private readonly LoggerInterface $logger,
30
        private readonly Pipeline $innerPipeline,
31
        private readonly Connection $middlewareConnection,
32
        private readonly Connection $gatewayConnection,
33
    ) {
34
    }
35
36
    public function process(AbstractCommand $command): AbstractCommand
37
    {
38
        $this->logger->debug(
39
            sprintf(
40
                'Starting Transaction in TransactionAwarePipeline for processing command "%s"',
41
                $command,
42
            ),
43
        );
44
45
        $this->middlewareConnection->beginTransaction();
46
        $this->gatewayConnection->beginTransaction();
47
48
        try {
49
            $this->logger->debug(sprintf('Requesting inner pipeline to process command "%s"', $command));
50
51
            $command = $this->innerPipeline->process($command);
52
53
            $this->logger->debug(sprintf('Inner pipeline processed command "%s", committing transaction', $command));
54
55
            $this->middlewareConnection->commit();
56
            $this->gatewayConnection->commit();
57
        } catch (Exception $e) {
58
            // log at highest level if we may have a split head in the db-cluster...
59
            if (strpos($e->getMessage(), 'ER_UNKNOWN_COM_ERROR')) {
60
                $this->logger->emergency(
61
                    sprintf(
62
                        '[!!!] Critical Database Exception while processing command "%s": "%s"',
63
                        $command,
64
                        $e->getMessage(),
65
                    ),
66
                    ['exception' => $e],
67
                );
68
            } else {
69
                $this->logger->error(
70
                    sprintf(
71
                        'Exception occurred while processing command "%s": "%s", rolling back transaction',
72
                        $command,
73
                        $e->getMessage(),
74
                    ),
75
                    ['exception' => $e],
76
                );
77
            }
78
79
            $this->middlewareConnection->rollBack();
80
            $this->gatewayConnection->rollBack();
81
82
            $this->logger->debug(
83
                sprintf(
84
                    'Transaction for command "%s" rolled back, re-throwing exception',
85
                    $command,
86
                ),
87
            );
88
89
            throw $e;
90
        }
91
92
        $this->logger->debug(sprintf('Transaction committed, done processing command "%s"', $command));
93
94
        return $command;
95
    }
96
}
97