Completed
Push — master ( 287393...7ff50d )
by Raffael
18:27 queued 14:12
created

Balloon.App.Notification/AttributeDecorator.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 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\Filesystem\Node\AttributeDecorator as NodeAttributeDecorator;
16
use Balloon\Server;
17
use Balloon\Server\AttributeDecorator as RoleAttributeDecorator;
18
use Closure;
19
use DateTime;
20
21
class AttributeDecorator implements AttributeDecoratorInterface
22
{
23
    /**
24
     * Server.
25
     *
26
     * @var Server
27
     */
28
    protected $server;
29
30
    /**
31
     * Role decorator.
32
     *
33
     * @var RoleAttributeDecorator
34
     */
35
    protected $role_decorator;
36
37
    /**
38
     * Node decorator.
39
     *
40
     * @var NodeAttributeDecorator
41
     */
42
    protected $node_decorator;
43
44
    /**
45
     * Custom attributes.
46
     *
47
     * @var array
48
     */
49
    protected $custom = [];
50
51
    /**
52
     * Init.
53
     */
54
    public function __construct(Server $server, NodeAttributeDecorator $node_decorator, RoleAttributeDecorator $role_decorator)
55
    {
56
        $this->server = $server;
57
        $this->node_decorator = $node_decorator;
58
        $this->role_decorator = $role_decorator;
59
    }
60
61
    /**
62
     * Decorate attributes.
63
     */
64 View Code Duplication
    public function decorate(array $message, array $attributes = []): array
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
65
    {
66
        $requested = $attributes;
67
        $attrs = $this->getAttributes($message);
68
69
        if (0 === count($requested)) {
70
            return $this->translateAttributes($message, $attrs);
71
        }
72
73
        return $this->translateAttributes($message, array_intersect_key($attrs, array_flip($requested)));
74
    }
75
76
    /**
77
     * Add decorator.
78
     */
79
    public function addDecorator(string $attribute, Closure $decorator): self
80
    {
81
        $this->custom[$attribute] = $decorator;
82
83
        return $this;
84
    }
85
86
    /**
87
     * Get Attributes.
88
     */
89
    protected function getAttributes(array $message): array
90
    {
91
        $server = $this->server;
92
        $role_decorator = $this->role_decorator;
93
        $fs = $this->server->getFilesystem();
94
        $node_decorator = $this->node_decorator;
95
96
        return [
97
            'id' => (string) $message['_id'],
98
            'message' => (string) $message['body'],
99
            'subject' => (string) $message['subject'],
100
            'locale' => (string) $message['locale'],
101 View Code Duplication
            'sender' => function ($message) use ($server, $role_decorator) {
0 ignored issues
show
This code seems to be duplicated across your project.

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.

Loading history...
102
                if (!isset($message['sender'])) {
103
                    return null;
104
                }
105
106
                try {
107
                    return $role_decorator->decorate($server->getUserById($message['sender']), ['_links', 'id', 'username']);
108
                } catch (\Exception $e) {
109
                    return null;
110
                }
111
            },
112
            'created' => function ($message) {
113
                return (new DateTime())->setTimestamp($message['_id']->getTimestamp())->format('c');
114
            },
115
            'node' => function ($message) use ($node_decorator, $fs) {
116
                if (isset($message['node'])) {
117
                    try {
118
                        return $node_decorator->decorate($fs->findNodeById($message['node']), ['id', 'name', '_links']);
119
                    } catch (\Exception $e) {
120
                        return null;
121
                    }
122
                }
123
            },
124
        ];
125
    }
126
127
    /**
128
     * Execute closures.
129
     */
130 View Code Duplication
    protected function translateAttributes(array $message, array $attributes): array
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
131
    {
132
        foreach ($attributes as $key => &$value) {
133
            if ($value instanceof Closure) {
134
                $result = $value->call($this, $message);
135
136
                if (null === $result) {
137
                    unset($attributes[$key]);
138
                } else {
139
                    $value = $result;
140
                }
141
            } elseif ($value === null) {
142
                unset($attributes[$key]);
143
            }
144
        }
145
146
        return $attributes;
147
    }
148
}
149