Skip to content

Commit cb5fdfd

Browse files
committed
Automatically split on push and tag
1 parent 0d9840d commit cb5fdfd

14 files changed

+109
-190
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
}
2626
],
2727
"require": {
28-
"php": "^7.4 || ^8.0",
29-
"beberlei/assert": "^3.0"
28+
"php": "^7.4 || ^8.0"
3029
},
3130
"require-dev": {
3231
"ergebnis/composer-normalize": "^2.11",

src/Envelope/DefaultEnvelope.php

Lines changed: 21 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,121 +2,74 @@
22

33
namespace SimpleBus\Serialization\Envelope;
44

5-
use Assert\Assertion;
5+
use LogicException;
66

77
class DefaultEnvelope implements Envelope
88
{
99
/**
10-
* @var string
10+
* @var class-string
1111
*/
12-
private $messageType;
12+
private string $messageType;
1313

14-
/**
15-
* @var null|object
16-
*/
17-
private $message;
14+
private ?object $message = null;
15+
16+
private ?string $serializedMessage = null;
1817

1918
/**
20-
* @var null|string
19+
* @param class-string $messageType
2120
*/
22-
private $serializedMessage;
23-
24-
protected function __construct($messageType, $message = null, $serializedMessage = null)
21+
protected function __construct(string $messageType, ?object $message, ?string $serializedMessage)
2522
{
26-
$this->setMessageType($messageType);
27-
$this->setMessage($message);
28-
$this->setSerializedMessage($serializedMessage);
23+
$this->messageType = $messageType;
24+
$this->message = $message;
25+
$this->serializedMessage = $serializedMessage;
2926
}
3027

31-
/**
32-
* {@inheritdoc}
33-
*/
34-
public static function forMessage($message)
28+
public static function forMessage(object $message): self
3529
{
3630
$type = get_class($message);
3731

3832
return new self($type, $message, null);
3933
}
4034

4135
/**
42-
* {@inheritdoc}
36+
* @param class-string $type
4337
*/
44-
public static function forSerializedMessage($type, $serializedMessage)
38+
public static function forSerializedMessage(string $type, string $serializedMessage): self
4539
{
46-
Assertion::string($type);
47-
Assertion::string($serializedMessage);
48-
4940
return new self($type, null, $serializedMessage);
5041
}
5142

52-
/**
53-
* {@inheritdoc}
54-
*/
55-
public function messageType()
43+
public function messageType(): string
5644
{
5745
return $this->messageType;
5846
}
5947

60-
/**
61-
* {@inheritdoc}
62-
*/
63-
public function message()
48+
public function message(): object
6449
{
6550
if (null === $this->message) {
66-
throw new \LogicException('Message is unavailable');
51+
throw new LogicException('Message is unavailable');
6752
}
6853

6954
return $this->message;
7055
}
7156

72-
/**
73-
* {@inheritdoc}
74-
*/
75-
public function serializedMessage()
57+
public function serializedMessage(): string
7658
{
7759
if (null === $this->serializedMessage) {
78-
throw new \LogicException('Serialized message is unavailable');
60+
throw new LogicException('Serialized message is unavailable');
7961
}
8062

8163
return $this->serializedMessage;
8264
}
8365

84-
/**
85-
* {@inheritdoc}
86-
*/
87-
public function withMessage($message)
66+
public function withMessage(object $message): self
8867
{
89-
Assertion::isObject($message);
90-
9168
return new self($this->messageType, $message, $this->serializedMessage);
9269
}
9370

94-
/**
95-
* {@inheritdoc}
96-
*/
97-
public function withSerializedMessage($serializedMessage)
71+
public function withSerializedMessage(string $serializedMessage): self
9872
{
99-
Assertion::string($serializedMessage);
100-
10173
return new self($this->messageType, $this->message, $serializedMessage);
10274
}
103-
104-
private function setMessageType($messageType)
105-
{
106-
Assertion::string($messageType);
107-
108-
$this->messageType = $messageType;
109-
}
110-
111-
private function setMessage($message = null)
112-
{
113-
$this->message = $message;
114-
}
115-
116-
private function setSerializedMessage($serializedMessage)
117-
{
118-
Assertion::nullOrString($serializedMessage);
119-
120-
$this->serializedMessage = $serializedMessage;
121-
}
12275
}

src/Envelope/DefaultEnvelopeFactory.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
class DefaultEnvelopeFactory implements EnvelopeFactory
66
{
7-
public function wrapMessageInEnvelope($message)
7+
public function wrapMessageInEnvelope(object $message): Envelope
88
{
99
return DefaultEnvelope::forMessage($message);
1010
}
1111

12-
public function envelopeClass()
12+
/**
13+
* @return class-string
14+
*/
15+
public function envelopeClass(): string
1316
{
14-
return 'SimpleBus\Serialization\Envelope\DefaultEnvelope';
17+
return DefaultEnvelope::class;
1518
}
1619
}

src/Envelope/Envelope.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,36 @@
22

33
namespace SimpleBus\Serialization\Envelope;
44

5+
use LogicException;
6+
57
interface Envelope
68
{
79
/**
810
* The type (FQCN) of the message.
911
*
10-
* @return string
12+
* @return class-string
1113
*/
12-
public function messageType();
14+
public function messageType(): string;
1315

1416
/**
1517
* The message.
1618
*
17-
* @throws \LogicException When the Message has not been provided
18-
*
19-
* @return object
19+
* @throws LogicException When the Message has not been provided
2020
*/
21-
public function message();
21+
public function message(): object;
2222

2323
/**
2424
* A new instance of the same class, with the same type, but another message.
25-
*
26-
* @param object $message
27-
*
28-
* @return Envelope
2925
*/
30-
public function withMessage($message);
26+
public function withMessage(object $message): Envelope;
3127

3228
/**
3329
* A new instance of the same class, with a serialized message of the same type.
34-
*
35-
* @param string $serializedMessage
36-
*
37-
* @return Envelope
3830
*/
39-
public function withSerializedMessage($serializedMessage);
31+
public function withSerializedMessage(string $serializedMessage): Envelope;
4032

4133
/**
42-
* @throws \LogicException When the serialized message has not been provided
43-
*
44-
* @return string
34+
* @throws LogicException When the serialized message has not been provided
4535
*/
46-
public function serializedMessage();
36+
public function serializedMessage(): string;
4737
}

src/Envelope/EnvelopeFactory.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@ interface EnvelopeFactory
66
{
77
/**
88
* Create an Envelope for a message.
9-
*
10-
* @param object $message
11-
*
12-
* @return Envelope
139
*/
14-
public function wrapMessageInEnvelope($message);
10+
public function wrapMessageInEnvelope(object $message): Envelope;
1511

1612
/**
1713
* The FQCN of the Envelope instances created by this factory.
1814
*
19-
* @return string
15+
* @return class-string
2016
*/
21-
public function envelopeClass();
17+
public function envelopeClass(): string;
2218
}

src/Envelope/Serializer/MessageInEnvelopeSerializer.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,11 @@ interface MessageInEnvelopeSerializer
88
{
99
/**
1010
* Serialize a Message to a string representation of that Message wrapped in an Envelope.
11-
*
12-
* @param object $message
13-
*
14-
* @return string
1511
*/
16-
public function wrapAndSerialize($message);
12+
public function wrapAndSerialize(object $message): string;
1713

1814
/**
1915
* Extract a Message from a serialized string representation of an Envelope.
20-
*
21-
* @param string $serializedEnvelope
22-
*
23-
* @return Envelope
2416
*/
25-
public function unwrapAndDeserialize($serializedEnvelope);
17+
public function unwrapAndDeserialize(string $serializedEnvelope): Envelope;
2618
}

src/Envelope/Serializer/StandardMessageInEnvelopeSerializer.php

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22

33
namespace SimpleBus\Serialization\Envelope\Serializer;
44

5+
use LogicException;
56
use SimpleBus\Serialization\Envelope\Envelope;
67
use SimpleBus\Serialization\Envelope\EnvelopeFactory;
78
use SimpleBus\Serialization\ObjectSerializer;
89

910
class StandardMessageInEnvelopeSerializer implements MessageInEnvelopeSerializer
1011
{
11-
/**
12-
* @var EnvelopeFactory
13-
*/
14-
private $envelopeFactory;
12+
private EnvelopeFactory $envelopeFactory;
1513

16-
/**
17-
* @var \SimpleBus\Serialization\ObjectSerializer
18-
*/
19-
private $objectSerializer;
14+
private ObjectSerializer $objectSerializer;
2015

2116
public function __construct(
2217
EnvelopeFactory $envelopeFactory,
@@ -28,10 +23,8 @@ public function __construct(
2823

2924
/**
3025
* Serialize a Message by wrapping it in an Envelope and serializing the envelope.
31-
*
32-
* {@inheritdoc}
3326
*/
34-
public function wrapAndSerialize($message)
27+
public function wrapAndSerialize(object $message): string
3528
{
3629
$envelope = $this->envelopeFactory->wrapMessageInEnvelope($message);
3730

@@ -42,10 +35,8 @@ public function wrapAndSerialize($message)
4235

4336
/**
4437
* Deserialize a Message that was wrapped in an Envelope.
45-
*
46-
* {@inheritdoc}
4738
*/
48-
public function unwrapAndDeserialize($serializedEnvelope)
39+
public function unwrapAndDeserialize(string $serializedEnvelope): Envelope
4940
{
5041
$envelope = $this->deserializeEnvelope($serializedEnvelope);
5142

@@ -56,12 +47,8 @@ public function unwrapAndDeserialize($serializedEnvelope)
5647

5748
/**
5849
* Deserialize the message Envelope.
59-
*
60-
* @param string $serializedEnvelope
61-
*
62-
* @return Envelope
6350
*/
64-
private function deserializeEnvelope($serializedEnvelope)
51+
private function deserializeEnvelope(string $serializedEnvelope): Envelope
6552
{
6653
$envelopeClass = $this->envelopeFactory->envelopeClass();
6754
$envelope = $this->objectSerializer->deserialize(
@@ -70,7 +57,11 @@ private function deserializeEnvelope($serializedEnvelope)
7057
);
7158

7259
if (!($envelope instanceof $envelopeClass)) {
73-
throw new \LogicException(sprintf('Expected deserialized object to be an instance of "%s"', $envelopeClass));
60+
throw new LogicException(sprintf('Expected deserialized object to be an instance of "%s"', $envelopeClass));
61+
}
62+
63+
if (!$envelope instanceof Envelope) {
64+
throw new LogicException(sprintf('Expected deserialized object to be an instance of "%s"', Envelope::class));
7465
}
7566

7667
return $envelope;
@@ -79,17 +70,14 @@ private function deserializeEnvelope($serializedEnvelope)
7970
/**
8071
* Deserialize the Message.
8172
*
82-
* @param string $serializedMessage
83-
* @param string $messageClass
84-
*
85-
* @return object Of type $messageClass
73+
* @param class-string $messageClass
8674
*/
87-
private function deserializeMessage($serializedMessage, $messageClass)
75+
private function deserializeMessage(string $serializedMessage, string $messageClass): object
8876
{
8977
$message = $this->objectSerializer->deserialize($serializedMessage, $messageClass);
9078

9179
if (!($message instanceof $messageClass)) {
92-
throw new \LogicException(sprintf('Expected deserialized message to be an instance of "%s"', $messageClass));
80+
throw new LogicException(sprintf('Expected deserialized message to be an instance of "%s"', $messageClass));
9381
}
9482

9583
return $message;

0 commit comments

Comments
 (0)