Completed
Push — master ( 9e4ce5...2301e9 )
by Maxim
02:29
created

Driver::setProject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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
        $this->bootListeners();
59 9
    }
60
61
    /**
62
     * Send notification.
63
     *
64
     * @param  Notification $notification
65
     * @return $this
66
     */
67 5
    public function send(Notification $notification)
68
    {
69 5
        $this->notification = $notification;
70
71 5
        return $this;
72
    }
73
74
    /**
75
     * Endpoints to send to.
76
     *
77
     * @param  array $subscribers
78
     * @return $this
79
     */
80 5
    public function to(array $subscribers)
81
    {
82 5
        $this->subscribers = $subscribers;
83
84 5
        return $this;
85
    }
86
87
    /**
88
     * Send notification.
89
     */
90 5
    public function flush()
91
    {
92 5
        $this->validate();
93
94 3
        $partials = $this->splitSubscribers();
95
96 3
        foreach ($partials as $provider => $subscribers) {
97
            try {
98 3
                $this->provider($provider)->send($this->notification, $subscribers);
99 3
            } catch (\RuntimeException $e) {
100
                static::emit('flush.exception', $e);
101
            }
102 3
        }
103 3
    }
104
105
    /**
106
     * Generate connection package.
107
     *
108
     * @param string $provider
109
     * @param array $extra
110
     */
111 1
    public function sendPackage($provider, $extra = [])
112
    {
113 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...
114
    }
115
116
    /**
117
     * Cast provider.
118
     *
119
     * @param  string $name
120
     * @return AbstractProvider
121
     * @throws \RuntimeException
122
     */
123 4
    public function provider($name)
124
    {
125 4
        return $this->project->getProvider($name);
126
    }
127
128
    /**
129
     * Prepare notifications.
130
     * Split subscribers by their providers and prepare payload.
131
     */
132 4
    protected function splitSubscribers()
133
    {
134 4
        $partials = [];
135
136
        /** @var Subscriber $subscriber */
137 4
        foreach ($this->subscribers as $subscriber) {
138 4
            $provider = $subscriber->getProvider();
139
140 4
            if (! $this->project->providerConnected($provider)) {
141 1
                continue;
142
            }
143
144 4
            if (! isset($partials[$provider])) {
145 4
                $partials[$provider] = [];
146 4
            }
147
148 4
            $partials[$provider][] = $subscriber;
149
150 4
            if ($this->payloadStorage) {
151 4
                $this->payloadStorage->assignNotificationToSubscriber(
152 4
                    $subscriber,
153 4
                    $this->notification,
154 4
                    $this->project->config['payload']['subscriber_lifetime']
155 4
                );
156 4
            }
157 4
        }
158
159 4
        return $partials;
160
    }
161
162
    /**
163
     * Validate data.
164
     *
165
     * @throws \RuntimeException
166
     */
167 5
    protected function validate()
168
    {
169 5
        if (is_null($this->notification)) {
170 1
            throw new \RuntimeException("Notification wasn't set.");
171
        }
172
173 4
        if (is_null($this->subscribers)) {
174 1
            throw new \RuntimeException('No subscribers set.');
175
        }
176 3
    }
177
178
    /**
179
     * Set project manually.
180
     *
181
     * @param  Project $project
182
     * @return $this
183
     */
184 1
    public function setProject(Project $project)
185
    {
186 1
        $this->project = $project;
187
188 1
        return $this;
189
    }
190
191
    /**
192
     * Get project instance.
193
     *
194
     * @return Project
195
     */
196 1
    public function getProject()
197
    {
198 1
        return $this->project;
199
    }
200
201
    /**
202
     * Boot event listeners.
203
     */
204 9
    protected function bootListeners()
205
    {
206 9
        if (! is_null($this->statisticsStorage)) {
207 9
            static::$events->useListenerProvider($this->statisticsStorage);
208 9
        }
209 9
    }
210
}
211