ResponseRecorder   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 99
ccs 48
cts 48
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A recordOn() 0 18 4
A attachRecordingCallbackToEventbus() 0 9 1
A getRecordingCallback() 0 10 2
A handleCommandDispatchException() 0 11 2
A handleShowableException() 0 11 1
A handleFatalException() 0 12 1
A logFatalError() 0 5 1
1
<?php declare(strict_types = 1);
2
3
namespace TomCizek\ResponseRecorder\Application;
4
5
use Prooph\Common\Event\ActionEvent;
6
use Prooph\Common\Messaging\DomainEvent;
7
use Prooph\ServiceBus\EventBus;
8
use Prooph\ServiceBus\Exception\CommandDispatchException;
9
use Prooph\ServiceBus\MessageBus;
10
use Psr\Log\LoggerInterface;
11
use Throwable;
12
use TomCizek\ResponseRecorder\Application\ResponseRecorder\BasicMutationResponse;
13
use TomCizek\ResponseRecorder\Application\ResponseRecorder\MutationResponse\BasicMutationResponseBuilder;
14
use TomCizek\ResponseRecorder\Application\ResponseRecorder\MutationResponse\ErrorCollection\GenericShowableMutationError;
15
use TomCizek\ResponseRecorder\DomainModel\ShowableTranslatableDomainException;
16
17
final class ResponseRecorder
18
{
19
	public const FATAL_ERROR_MESSAGE = 'fatal_error_occured_logged';
20
21
	/** @var EventBus */
22
	private $eventBus;
23
24
	/** @var LoggerInterface */
25
	private $logger;
26
27 9
	public function __construct(EventBus $eventBus, LoggerInterface $logger)
28
	{
29 9
		$this->eventBus = $eventBus;
30 9
		$this->logger = $logger;
31 9
	}
32
33 9
	public function recordOn($callback): BasicMutationResponse
34
	{
35 9
		$responseBuilder = BasicMutationResponseBuilder::create();
36
37 9
		$this->attachRecordingCallbackToEventbus($responseBuilder);
38
39
		try {
40 9
			$callback();
41 5
		} catch (CommandDispatchException $exception) {
0 ignored issues
show
Bug introduced by
The class Prooph\ServiceBus\Except...ommandDispatchException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
42 2
			$this->handleCommandDispatchException($exception, $responseBuilder);
43 3
		} catch (ShowableTranslatableDomainException $exception) {
44 1
			$this->handleShowableException($exception, $responseBuilder);
45 2
		} catch (Throwable $exception) {
46 2
			$this->handleFatalException($exception, $responseBuilder);
47
		}
48
49 9
		return $responseBuilder->build();
50
	}
51
52 9
	private function attachRecordingCallbackToEventbus(
53
		BasicMutationResponseBuilder $responseBuilder
54
	): void {
55 9
		$this->eventBus->attach(
56 9
			EventBus::EVENT_DISPATCH,
57 9
			$this->getRecordingCallback($responseBuilder),
58 9
			-1000
59
		);
60 9
	}
61
62
	private function getRecordingCallback(BasicMutationResponseBuilder $responseBuilder): \Closure
63
	{
64 9
		return function (ActionEvent $actionEvent) use ($responseBuilder): void {
65 4
			$event = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE);
66 4
			if (!$event instanceof DomainEvent) {
0 ignored issues
show
Bug introduced by
The class Prooph\Common\Messaging\DomainEvent does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
67 1
				return;
68
			}
69 3
			$responseBuilder->addDomainEvent($event);
70 9
		};
71
	}
72
73 2
	private function handleCommandDispatchException(
74
		CommandDispatchException $exception,
75
		BasicMutationResponseBuilder $responseBuilder
76
	): void {
77 2
		$previous = $exception->getPrevious();
78 2
		if ($previous instanceof ShowableTranslatableDomainException) {
79 1
			$this->handleShowableException($previous, $responseBuilder);
80
		} else {
81 1
			$this->handleFatalException($exception, $responseBuilder);
82
		}
83 2
	}
84
85 2
	protected function handleShowableException(
86
		ShowableTranslatableDomainException $exception,
87
		BasicMutationResponseBuilder $responseBuilder
88
	): void {
89 2
		$responseBuilder->addError(
90 2
			GenericShowableMutationError::create(
91 2
				$exception->getMessage(),
92 2
				$exception->getMessageParameters()
93
			)
94
		);
95 2
	}
96
97 3
	protected function handleFatalException(
98
		Throwable $exception,
99
		BasicMutationResponseBuilder $responseBuilder
100
	): void {
101 3
		$this->logFatalError($exception);
102
103 3
		$responseBuilder->addError(
104 3
			GenericShowableMutationError::create(
105 3
				self::FATAL_ERROR_MESSAGE
106
			)
107
		);
108 3
	}
109
110 3
	private function logFatalError(
111
		Throwable $exception
112
	): void {
113 3
		$this->logger->error($exception);
114 3
	}
115
}
116