Completed
Push — master ( 4ba3cf...18ed91 )
by Kevin
02:35
created

Publisher::generateMetadata()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4286
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Zenstruck\Queue;
4
5
use SimpleBus\Asynchronous\Publisher\Publisher as SimpleBusPublisher;
6
use SimpleBus\Serialization\Envelope\Serializer\MessageInEnvelopSerializer;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
final class Publisher implements SimpleBusPublisher
12
{
13
    private $driver;
14
    private $serializer;
15
16 3
    public function __construct(Driver $driver, MessageInEnvelopSerializer $serializer)
17
    {
18 3
        $this->driver = $driver;
19 3
        $this->serializer = $serializer;
20 3
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 3
    public function publish($message)
26
    {
27 3
        \Assert\that($message)->isObject();
28
29 3
        $this->driver->push(
30 3
            new Payload($this->serializer->wrapAndSerialize($message), $this->generateMetadata($message))
31 3
        );
32 3
    }
33
34
    /**
35
     * @param object $message
36
     *
37
     * @return string
38
     */
39 3
    private function generateMetadata($message)
40
    {
41 3
        $class = get_class($message);
42
43 3
        if ($message instanceof \JsonSerializable) {
44 1
            return sprintf('%s: %s', $class, json_encode($message));
45
        }
46
47 2
        if (method_exists($message, '__toString')) {
48 1
            return sprintf('%s: %s', $class, $message);
49
        }
50
51 1
        return $class;
52
    }
53
}
54