1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* balloon |
7
|
|
|
* |
8
|
|
|
* @copyright Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Balloon\App\Notification; |
13
|
|
|
|
14
|
|
|
use Balloon\AttributeDecorator\AttributeDecoratorInterface; |
15
|
|
|
use Balloon\Server; |
16
|
|
|
use Balloon\Server\AttributeDecorator as RoleAttribueDecorator; |
17
|
|
|
use Closure; |
18
|
|
|
|
19
|
|
|
class AttributeDecorator implements AttributeDecoratorInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Server. |
23
|
|
|
* |
24
|
|
|
* @var Server |
25
|
|
|
*/ |
26
|
|
|
protected $server; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Role decorator. |
30
|
|
|
* |
31
|
|
|
* @var RoleAttribueDecorator |
32
|
|
|
*/ |
33
|
|
|
protected $role_decorator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Custom attributes. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $custom = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Init. |
44
|
|
|
* |
45
|
|
|
* @param Server $server |
46
|
|
|
* @param RoleAttribueDecorator $role_decorator |
47
|
|
|
*/ |
48
|
|
|
public function __construct(Server $server, RoleAttribueDecorator $role_decorator) |
49
|
|
|
{ |
50
|
|
|
$this->server = $server; |
51
|
|
|
$this->role_decorator = $role_decorator; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Decorate attributes. |
56
|
|
|
* |
57
|
|
|
* @param array $message |
58
|
|
|
* @param array $attributes |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function decorate(array $message, array $attributes = []): array |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$requested = $attributes; |
65
|
|
|
$attrs = $this->getAttributes($message); |
66
|
|
|
|
67
|
|
|
if (0 === count($requested)) { |
68
|
|
|
return $this->translateAttributes($message, $attrs); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->translateAttributes($message, array_intersect_key($attrs, array_flip($requested))); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Add decorator. |
76
|
|
|
* |
77
|
|
|
* @param string $attribute |
78
|
|
|
* @param Closure $decorator |
79
|
|
|
* |
80
|
|
|
* @return AttributeDecorator |
81
|
|
|
*/ |
82
|
|
|
public function addDecorator(string $attribute, Closure $decorator): self |
83
|
|
|
{ |
84
|
|
|
$this->custom[$attribute] = $decorator; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get Attributes. |
91
|
|
|
* |
92
|
|
|
* @param array $message |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
protected function getAttributes(array $message): array |
97
|
|
|
{ |
98
|
|
|
$server = $this->server; |
99
|
|
|
$role_decorator = $this->role_decorator; |
100
|
|
|
|
101
|
|
|
return [ |
102
|
|
|
'id' => (string) $message['_id'], |
103
|
|
|
'message' => (string) $message['body'], |
104
|
|
|
'subject' => (string) $message['subject'], |
105
|
|
View Code Duplication |
'sender' => function ($message) use ($server, $role_decorator) { |
|
|
|
|
106
|
|
|
try { |
107
|
|
|
return $role_decorator->decorate($server->getUserById($message['sender']), ['_links', 'id', 'username']); |
108
|
|
|
} catch (\Exception $e) { |
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
}, |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Execute closures. |
117
|
|
|
* |
118
|
|
|
* @param array $message |
119
|
|
|
* @param array $attributes |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
protected function translateAttributes(array $message, array $attributes): array |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
foreach ($attributes as $key => &$value) { |
126
|
|
|
if ($value instanceof Closure) { |
127
|
|
|
$result = $value->call($this, $message); |
128
|
|
|
|
129
|
|
|
if (null === $result) { |
130
|
|
|
unset($attributes[$key]); |
131
|
|
|
} else { |
132
|
|
|
$value = $result; |
133
|
|
|
} |
134
|
|
|
} elseif ($value === null) { |
135
|
|
|
unset($attributes[$key]); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $attributes; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.