getMessageConverter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php declare(strict_types = 1);
2
3
namespace TomCizek\SymfonyProoph\AsynchronousMessages\Factories;
4
5
use Interop\Config\ConfigurationTrait;
6
use Interop\Config\ProvidesDefaultOptions;
7
use Interop\Config\RequiresConfigId;
8
use InvalidArgumentException;
9
use Prooph\Common\Messaging\MessageConverter;
10
use Psr\Container\ContainerInterface;
11
use TomCizek\SymfonyProoph\AsynchronousMessages\AsynchronousMessageProducer;
12
use TomCizek\SymfonyProoph\AsynchronousMessages\AsynchronousMessageProducerBridge;
13
14
abstract class AbstractAsynchronousMessageProducerFactory implements RequiresConfigId, ProvidesDefaultOptions
15
{
16
	use ConfigurationTrait;
17
	public const KEY_BRIDGE = 'bridge';
18
	public const KEY_ROUTES = 'routes';
19
20
	/**
21
	 * @var string
22
	 */
23
	private $configId;
24
25 4
	public function __construct(string $configId)
26
	{
27 4
		$this->configId = $configId;
28 4
	}
29
30
	/**
31
	 * Creates a new instance from a specified config, specifically meant to be used as static factory.
32
	 * @throws InvalidArgumentException
33
	 */
34 5
	public static function __callStatic(string $name, array $arguments): AsynchronousMessageProducer
35
	{
36 5
		if (!isset($arguments[0]) || !$arguments[0] instanceof ContainerInterface) {
37 1
			throw new InvalidArgumentException(
38 1
				sprintf('The first argument must be of type %s', ContainerInterface::class)
39
			);
40
		}
41
42 4
		return (new static($name))->__invoke($arguments[0]);
43
	}
44
45 4
	public function __invoke(ContainerInterface $container): AsynchronousMessageProducer
46
	{
47 4
		$producerConfig = $this->getProducerconfg($container);
48
49 4
		$producer = $this->createMessageProducer($container, $producerConfig);
50
51 4
		$this->injectRoutesToProducer($producerConfig, $producer);
52
53 4
		return $producer;
54
	}
55
56 4
	public function dimensions(): iterable
57
	{
58 4
		return ['prooph', 'asynchronous_messaging'];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('prooph', 'asynchronous_messaging'); (string[]) is incompatible with the return type declared by the interface Interop\Config\RequiresConfig::dimensions of type Interop\Config\iterable.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
59
	}
60
61 4
	public function defaultOptions(): iterable
62
	{
63 4
		return [];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type declared by the interface Interop\Config\ProvidesD...Options::defaultOptions of type Interop\Config\iterable.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
64
	}
65
66 4
	private function getProducerconfg(ContainerInterface $container): array
67
	{
68 4
		$config = $container->get('config');
69
70 4
		return $this->optionsWithFallback($config, $this->configId);
71
	}
72
73 4
	private function createMessageProducer(
74
		ContainerInterface $container,
75
		array $producerConfig
76
	): AsynchronousMessageProducer {
77 4
		$producerBridge = $this->getProducerBridge($container, $producerConfig);
78 4
		$messageConverter = $this->getMessageConverter($container);
79
80 4
		return new AsynchronousMessageProducer($producerBridge, $messageConverter);
81
	}
82
83 4
	private function getProducerBridge(
84
		ContainerInterface $container,
85
		$producerConfig
86
	): AsynchronousMessageProducerBridge {
87 4
		$producerBridgeKey = $this->getProducerBridgeServiceKey($producerConfig);
88
89 4
		return $container->get($producerBridgeKey);
90
	}
91
92 4
	private function getProducerBridgeServiceKey(array $producerConfig): string
93
	{
94 4
		return $producerConfig[self::KEY_BRIDGE];
95
	}
96
97 4
	private function getMessageConverter(ContainerInterface $container): MessageConverter
98
	{
99 4
		return $container->get(MessageConverter::class);
100
	}
101
102 4
	private function injectRoutesToProducer(array $producerConfig, AsynchronousMessageProducer $producer): void
103
	{
104 4
		$routes = is_array($producerConfig[self::KEY_ROUTES]) ? $producerConfig[self::KEY_ROUTES] : [];
105 4
		$producer->injectRoutes($routes);
106 4
	}
107
}
108