Completed
Push — master ( 21f279...d6aafa )
by Maxim
02:34
created

Driver::bootListeners()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Notimatica\Driver;
4
5
use Notimatica\Driver\Contracts\Notification;
6
use Notimatica\Driver\Contracts\PayloadStorage as PayloadStorageContract;
7
use Notimatica\Driver\Contracts\Subscriber;
8
use Notimatica\Driver\Providers\AbstractProvider;
9
use Notimatica\Driver\Support\EventsEmitter;
10
11
class Driver
12
{
13
    use EventsEmitter;
14
15
    /**
16
     * @var Project
17
     */
18
    protected $project;
19
    /**
20
     * @var Notification
21
     */
22
    protected $notification;
23
    /**
24
     * @var array
25
     */
26
    protected $subscribers;
27
    /**
28
     * @var PayloadStorageContract
29
     */
30
    protected $payloadStorage;
31
    /**
32
     * @var Statistics
33
     */
34
    protected $statisticsStorage;
35
36
    /**
37
     * Create a new Driver.
38
     *
39
     * @param Project $project
40
     * @param PayloadStorageContract $payloadStorage
41
     * @param Statistics $statisticsStorage
42
     */
43 9
    public function __construct(Project $project, PayloadStorageContract $payloadStorage = null, Statistics $statisticsStorage = null)
44
    {
45 9
        $this->project = $project;
46 9
        $this->payloadStorage = $payloadStorage;
47 9
        $this->statisticsStorage = $statisticsStorage;
48
49 9
        $this->boot();
50 9
    }
51
52
    /**
53
     * Boot driver.
54
     */
55 9
    public function boot()
56
    {
57 9
        $this->bootEvents();
58 9
    }
59
60
    /**
61
     * Send notification.
62
     *
63
     * @param  Notification $notification
64
     * @return $this
65
     */
66 5
    public function send(Notification $notification)
67
    {
68 5
        $this->notification = $notification;
69
70 5
        return $this;
71
    }
72
73
    /**
74
     * Endpoints to send to.
75
     *
76
     * @param  array $subscribers
77
     * @return $this
78
     */
79 5
    public function to(array $subscribers)
80
    {
81 5
        $this->subscribers = $subscribers;
82
83 5
        return $this;
84
    }
85
86
    /**
87
     * Send notification.
88
     */
89 5
    public function flush()
90
    {
91 5
        $this->validate();
92
93 3
        $partials = $this->splitSubscribers();
94
95 3
        foreach ($partials as $provider => $subscribers) {
96
            try {
97 3
                $this->provider($provider)->send($this->notification, $subscribers);
98 3
            } catch (\RuntimeException $e) {
99
                static::emit('flush.exception', $e);
100
            }
101 3
        }
102 3
    }
103
104
    /**
105
     * Generate connection package.
106
     *
107
     * @param string $provider
108
     * @param array $extra
109
     */
110 1
    public function sendPackage($provider, $extra = [])
111
    {
112 1
        return $this->provider($provider)->connectionPackage($extra);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Notimatica\Driver\Providers\AbstractProvider as the method connectionPackage() does only exist in the following sub-classes of Notimatica\Driver\Providers\AbstractProvider: Notimatica\Driver\Providers\Safari. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
113
    }
114
115
    /**
116
     * Cast provider.
117
     *
118
     * @param  string $name
119
     * @return AbstractProvider
120
     * @throws \RuntimeException
121
     */
122 4
    public function provider($name)
123
    {
124 4
        return $this->project->getProvider($name);
125
    }
126
127
    /**
128
     * Prepare notifications.
129
     * Split subscribers by their providers and prepare payload.
130
     */
131 4
    protected function splitSubscribers()
132
    {
133 4
        $partials = [];
134
135
        /** @var Subscriber $subscriber */
136 4
        foreach ($this->subscribers as $subscriber) {
137 4
            $provider = $subscriber->getProvider();
138
139 4
            if (! $this->project->providerConnected($provider)) {
140 1
                continue;
141
            }
142
143 4
            if (! isset($partials[$provider])) {
144 4
                $partials[$provider] = [];
145 4
            }
146
147 4
            $partials[$provider][] = $subscriber;
148
149 4
            if ($this->payloadStorage) {
150 4
                $this->payloadStorage->assignNotificationToSubscriber(
151 4
                    $subscriber,
152 4
                    $this->notification,
153 4
                    $this->project->config['payload']['subscriber_lifetime']
154 4
                );
155 4
            }
156 4
        }
157
158 4
        return $partials;
159
    }
160
161
    /**
162
     * Validate data.
163
     *
164
     * @throws \RuntimeException
165
     */
166 5
    protected function validate()
167
    {
168 5
        if (is_null($this->notification)) {
169 1
            throw new \RuntimeException("Notification wasn't set.");
170
        }
171
172 4
        if (is_null($this->subscribers)) {
173 1
            throw new \RuntimeException('No subscribers set.');
174
        }
175 3
    }
176
177
    /**
178
     * Set project manually.
179
     *
180
     * @param  Project $project
181
     * @return $this
182
     */
183 1
    public function setProject(Project $project)
184
    {
185 1
        $this->project = $project;
186
187 1
        return $this;
188
    }
189
190
    /**
191
     * Get project instance.
192
     *
193
     * @return Project
194
     */
195 1
    public function getProject()
196
    {
197 1
        return $this->project;
198
    }
199
200
    /**
201
     * Boot event listeners.
202
     */
203 9
    protected function bootListeners()
204
    {
205 9
        if (! is_null($this->statisticsStorage)) {
206 9
            static::$events->useListenerProvider($this->statisticsStorage);
207 9
        }
208 9
    }
209
}
210