Completed
Branch v1 (ffe92e)
by Julián
02:44
created

Service::feedback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 2
eloc 12
nc 2
nop 0
1
<?php
2
/**
3
 * Push notification services abstraction (http://github.com/juliangut/tify)
4
 *
5
 * @link https://github.com/juliangut/tify for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Tify;
11
12
use Doctrine\Common\Collections\ArrayCollection;
13
use Jgut\Tify\Adapter\AbstractAdapter;
14
use Jgut\Tify\Adapter\FeedbackAdapter;
15
use Jgut\Tify\Adapter\SendAdapter;
16
17
/**
18
 * Notifications service.
19
 */
20
class Service
21
{
22
    /**
23
     * @var \Jgut\Tify\Adapter\AbstractAdapter
24
     */
25
    protected $adapters;
26
27
    /**
28
     * @var \Jgut\Tify\Notification[]
29
     */
30
    protected $notifications;
31
32
    /**
33
     * Manager constructor.
34
     *
35
     * @param \Jgut\Tify\Adapter\AbstractAdapter[] $adapters
36
     * @param \Jgut\Tify\Notification[]            $notifications
37
     */
38
    public function __construct(array $adapters = [], array $notifications = [])
39
    {
40
        $this->adapters = new ArrayCollection;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type object<Jgut\Tify\Adapter\AbstractAdapter> of property $adapters.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
        foreach ($adapters as $adapter) {
42
            $this->addAdapter($adapter);
43
        }
44
45
        $this->notifications = new ArrayCollection;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type array<integer,object<Jgut\Tify\Notification>> of property $notifications.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
        foreach ($notifications as $notification) {
47
            $this->addNotification($notification);
48
        }
49
    }
50
51
    /**
52
     * Retrieve registered adapters.
53
     *
54
     * @return \Jgut\Tify\Adapter\AbstractAdapter[]
55
     */
56
    public function getAdapters()
57
    {
58
        return $this->adapters->toArray();
0 ignored issues
show
Bug introduced by
The method toArray() does not seem to exist on object<Jgut\Tify\Adapter\AbstractAdapter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
    }
60
61
    /**
62
     * Register adapter.
63
     *
64
     * @param \Jgut\Tify\Adapter\AbstractAdapter $adapter
65
     *
66
     * @return $this
67
     */
68
    public function addAdapter(AbstractAdapter $adapter)
69
    {
70
        $this->adapters->add($adapter);
0 ignored issues
show
Bug introduced by
The method add() does not seem to exist on object<Jgut\Tify\Adapter\AbstractAdapter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
72
        return $this;
73
    }
74
75
    /**
76
     * Clear list of adapters.
77
     */
78
    public function clearAdapters()
79
    {
80
        $this->adapters->clear();
0 ignored issues
show
Bug introduced by
The method clear() does not seem to exist on object<Jgut\Tify\Adapter\AbstractAdapter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
82
        return $this;
83
    }
84
85
    /**
86
     * Retrieve registered notifications.
87
     *
88
     * @return \Jgut\Tify\Notification[]
89
     */
90
    public function getNotifications()
91
    {
92
        return $this->notifications->toArray();
0 ignored issues
show
Bug introduced by
The method toArray cannot be called on $this->notifications (of type array<integer,object<Jgut\Tify\Notification>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
93
    }
94
95
    /**
96
     * Register notification.
97
     *
98
     * @param \Jgut\Tify\Notification $notification
99
     *
100
     * @return $this
101
     */
102
    public function addNotification(Notification $notification)
103
    {
104
        $this->notifications->add($notification);
0 ignored issues
show
Bug introduced by
The method add cannot be called on $this->notifications (of type array<integer,object<Jgut\Tify\Notification>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
105
106
        return $this;
107
    }
108
109
    /**
110
     * Clear list of notifications.
111
     */
112
    public function clearNotifications()
113
    {
114
        $this->notifications->clear();
0 ignored issues
show
Bug introduced by
The method clear cannot be called on $this->notifications (of type array<integer,object<Jgut\Tify\Notification>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
115
116
        return $this;
117
    }
118
119
    /**
120
     * Push notifications.
121
     *
122
     * @return \Jgut\Tify\Result[]
123
     */
124
    public function push()
125
    {
126
        $results = [];
127
128
        /** @var \Jgut\Tify\Adapter\SendAdapter[] $pushAdapters */
129
        $pushAdapters = array_filter(
130
            $this->adapters->toArray(),
0 ignored issues
show
Bug introduced by
The method toArray() does not seem to exist on object<Jgut\Tify\Adapter\AbstractAdapter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
            function (AbstractAdapter $adapter) {
132
                return $adapter instanceof SendAdapter;
133
            }
134
        );
135
136
        foreach ($pushAdapters as $adapter) {
137
            foreach ($this->notifications as $notification) {
138
                $notification->clearResults();
139
140
                $adapter->send($notification);
141
142
                $results[] = $notification->getResults();
143
            }
144
        }
145
146
        $return = [];
147
        array_walk_recursive($results, function ($current) use (&$return) {
148
            $return[] = $current;
149
        });
150
151
        return $return;
152
    }
153
154
    /**
155
     * Get feedback from push services.
156
     *
157
     * @return array
158
     */
159
    public function feedback()
160
    {
161
        $results = [];
162
163
        /** @var \Jgut\Tify\Adapter\FeedbackAdapter[] $feedbackAdapters */
164
        $feedbackAdapters = array_filter(
165
            $this->adapters->toArray(),
0 ignored issues
show
Bug introduced by
The method toArray() does not seem to exist on object<Jgut\Tify\Adapter\AbstractAdapter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
166
            function (AbstractAdapter $adapter) {
167
                return $adapter instanceof FeedbackAdapter;
168
            }
169
        );
170
171
        foreach ($feedbackAdapters as $adapter) {
172
            $results[] = $adapter->feedback();
173
        }
174
175
        $return = [];
176
        array_walk_recursive($results, function ($current) use (&$return) {
177
            $return[] = $current;
178
        });
179
180
        return array_unique($return);
181
    }
182
}
183