1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 Underground Elephant |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
* |
18
|
|
|
* @package qpush-bundle |
19
|
|
|
* @copyright Underground Elephant 2014 |
20
|
|
|
* @license Apache License, Version 2.0 |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Uecode\Bundle\QPushBundle\Message; |
24
|
|
|
|
25
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author Keith Kirk <[email protected]> |
29
|
|
|
*/ |
30
|
|
View Code Duplication |
class Message |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Message Id |
34
|
|
|
* |
35
|
|
|
* @var int|string |
36
|
|
|
*/ |
37
|
|
|
protected $id; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Message Body |
41
|
|
|
* |
42
|
|
|
* @var string|array |
43
|
|
|
*/ |
44
|
|
|
protected $body; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Message Metadata |
48
|
|
|
* |
49
|
|
|
* @var ArrayCollection |
50
|
|
|
*/ |
51
|
|
|
protected $metadata; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Constructor. |
55
|
|
|
* |
56
|
|
|
* Sets the Message Id, Message Body, and any Message Metadata |
57
|
|
|
* |
58
|
|
|
* @param int|string $id The Message Id |
59
|
|
|
* @param string|array $body The Message Message |
60
|
|
|
* @param array $metadata The Message Metadata |
61
|
|
|
*/ |
62
|
19 |
|
public function __construct($id, $body, array $metadata) |
63
|
|
|
{ |
64
|
19 |
|
$this->id = $id; |
65
|
19 |
|
$this->metadata = new ArrayCollection($metadata); |
66
|
|
|
|
67
|
19 |
|
$message = is_string($body) ? json_decode($body, true) : $body; |
68
|
19 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
69
|
4 |
|
$message = $body; |
70
|
|
|
} |
71
|
|
|
|
72
|
19 |
|
$this->body = $message; |
73
|
19 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the Message Id |
77
|
|
|
* |
78
|
|
|
* @return int|string |
79
|
|
|
*/ |
80
|
5 |
|
public function getId() |
81
|
|
|
{ |
82
|
5 |
|
return $this->id; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the Message Body |
87
|
|
|
* |
88
|
|
|
* @return string|array |
89
|
|
|
*/ |
90
|
4 |
|
public function getBody() |
91
|
|
|
{ |
92
|
4 |
|
return $this->body; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the Message Metadata |
97
|
|
|
* |
98
|
|
|
* @return ArrayCollection |
99
|
|
|
*/ |
100
|
3 |
|
public function getMetadata() |
101
|
|
|
{ |
102
|
3 |
|
return $this->metadata; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.