1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Messaging\StringMessage |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/messaging |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Messaging; |
22
|
|
|
|
23
|
|
|
use Rhumsaa\Uuid\Uuid; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The implementation for sending a message containing |
27
|
|
|
* data encapsulated in a string. |
28
|
|
|
* |
29
|
|
|
* @author Tim Wagner <[email protected]> |
30
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
32
|
|
|
* @link https://github.com/appserver-io/messaging |
33
|
|
|
* @link http://www.appserver.io |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
class StringMessage extends AbstractMessage |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The message id as hash value. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $messageId = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The message itself. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $message = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Initializes the message with the String |
54
|
|
|
* to send to the queue. |
55
|
|
|
* |
56
|
|
|
* @param string $message The string with the data to send |
57
|
|
|
* |
58
|
|
|
* @throws \Exception Is thrown if the passed message is not a valid string value |
59
|
|
|
*/ |
60
|
1 |
|
public function __construct($message) |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
// call parent constructor |
64
|
1 |
|
parent::__construct(); |
65
|
|
|
|
66
|
|
|
// check if we've an string passed |
67
|
1 |
|
if (is_string($message) === false) { |
68
|
|
|
throw new \Exception(sprintf('Message \'%s\' is not a valid string', $message)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// initialize the String sent with the message |
72
|
1 |
|
$this->message = $message; |
73
|
|
|
// initialize the message-ID |
74
|
1 |
|
$this->messageId = Uuid::uuid4()->__toString(); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the unique message-ID. |
79
|
|
|
* |
80
|
|
|
* @return string The unique message-ID |
81
|
|
|
*/ |
82
|
|
|
public function getMessageId() |
83
|
|
|
{ |
84
|
|
|
return $this->messageId; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* The message itself. |
89
|
|
|
* |
90
|
|
|
* @return string The message itself |
91
|
|
|
*/ |
92
|
|
|
public function getMessage() |
93
|
|
|
{ |
94
|
|
|
return $this->message; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the message as string. |
99
|
|
|
* |
100
|
|
|
* @return string The message as string |
101
|
|
|
*/ |
102
|
|
|
public function __toString() |
103
|
|
|
{ |
104
|
|
|
return $this->message; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.