| @@ -83,10 +83,14 @@ discard block | ||
| 83 | 83 | |
| 84 | 84 | $partials = $this->splitSubscribers(); | 
| 85 | 85 | |
| 86 | -        foreach ($partials as $provider => $subscribers) { | |
| 87 | -            try { | |
| 86 | + foreach ($partials as $provider => $subscribers) | |
| 87 | +        { | |
| 88 | + try | |
| 89 | +            { | |
| 88 | 90 | $this->provider($provider)->send($this->notification, $subscribers); | 
| 89 | -            } catch (\RuntimeException $e) { | |
| 91 | + } | |
| 92 | + catch (\RuntimeException $e) | |
| 93 | +            { | |
| 90 | 94 |                  static::emit('flush.exception', $e); | 
| 91 | 95 | } | 
| 92 | 96 | } | 
| @@ -124,20 +128,24 @@ discard block | ||
| 124 | 128 | $partials = []; | 
| 125 | 129 | |
| 126 | 130 | /** @var Subscriber $subscriber */ | 
| 127 | -        foreach ($this->subscribers as $subscriber) { | |
| 131 | + foreach ($this->subscribers as $subscriber) | |
| 132 | +        { | |
| 128 | 133 | $provider = $subscriber->getProvider(); | 
| 129 | 134 | |
| 130 | -            if (! $this->project->providerConnected($provider)) { | |
| 135 | + if (! $this->project->providerConnected($provider)) | |
| 136 | +            { | |
| 131 | 137 | continue; | 
| 132 | 138 | } | 
| 133 | 139 | |
| 134 | -            if (! isset($partials[$provider])) { | |
| 140 | + if (! isset($partials[$provider])) | |
| 141 | +            { | |
| 135 | 142 | $partials[$provider] = []; | 
| 136 | 143 | } | 
| 137 | 144 | |
| 138 | 145 | $partials[$provider][] = $subscriber; | 
| 139 | 146 | |
| 140 | -            if ($this->payloadStorage) { | |
| 147 | + if ($this->payloadStorage) | |
| 148 | +            { | |
| 141 | 149 | $this->payloadStorage->assignNotificationToSubscriber($subscriber, $this->notification); | 
| 142 | 150 | } | 
| 143 | 151 | } | 
| @@ -152,11 +160,13 @@ discard block | ||
| 152 | 160 | */ | 
| 153 | 161 | protected function validate() | 
| 154 | 162 |      { | 
| 155 | -        if (is_null($this->notification)) { | |
| 163 | + if (is_null($this->notification)) | |
| 164 | +        { | |
| 156 | 165 |              throw new \RuntimeException("Notification wasn't set."); | 
| 157 | 166 | } | 
| 158 | 167 | |
| 159 | -        if (is_null($this->subscribers)) { | |
| 168 | + if (is_null($this->subscribers)) | |
| 169 | +        { | |
| 160 | 170 |              throw new \RuntimeException('No subscribers set.'); | 
| 161 | 171 | } | 
| 162 | 172 | } | 
| @@ -49,16 +49,20 @@ | ||
| 49 | 49 | */ | 
| 50 | 50 | public function provideListeners(ListenerAcceptorInterface $listener) | 
| 51 | 51 |      { | 
| 52 | -        $listener->addListener(NotificationSent::class, function ($e) { | |
| 52 | + $listener->addListener(NotificationSent::class, function ($e) | |
| 53 | +        { | |
| 53 | 54 | $this->sent($e); | 
| 54 | 55 | }); | 
| 55 | -        $listener->addListener(NotificationDelivered::class, function ($e) { | |
| 56 | + $listener->addListener(NotificationDelivered::class, function ($e) | |
| 57 | +        { | |
| 56 | 58 | $this->delivered($e); | 
| 57 | 59 | }); | 
| 58 | -        $listener->addListener(NotificationClicked::class, function ($e) { | |
| 60 | + $listener->addListener(NotificationClicked::class, function ($e) | |
| 61 | +        { | |
| 59 | 62 | $this->clicked($e); | 
| 60 | 63 | }); | 
| 61 | -        $listener->addListener(NotificationFailed::class, function ($e) { | |
| 64 | + $listener->addListener(NotificationFailed::class, function ($e) | |
| 65 | +        { | |
| 62 | 66 | $this->failed($e); | 
| 63 | 67 | }); | 
| 64 | 68 | |
| @@ -57,7 +57,8 @@ discard block | ||
| 57 | 57 | */ | 
| 58 | 58 | public function getProvider($name) | 
| 59 | 59 |      { | 
| 60 | -        if (! $this->providerConnected($name)) { | |
| 60 | + if (! $this->providerConnected($name)) | |
| 61 | +        { | |
| 61 | 62 |              throw new \RuntimeException("Unsupported provider '{$name}'"); | 
| 62 | 63 | } | 
| 63 | 64 | |
| @@ -82,8 +83,10 @@ discard block | ||
| 82 | 83 |      { | 
| 83 | 84 | $providersFactory = new ProvidersFactory($this); | 
| 84 | 85 | |
| 85 | -        if (! empty($this->config['providers']) && is_array($this->config['providers'])) { | |
| 86 | -            foreach ($this->config['providers'] as $name => $options) { | |
| 86 | + if (! empty($this->config['providers']) && is_array($this->config['providers'])) | |
| 87 | +        { | |
| 88 | + foreach ($this->config['providers'] as $name => $options) | |
| 89 | +            { | |
| 87 | 90 | $this->providers[$name] = $providersFactory->make($name, $options)->setProject($this); | 
| 88 | 91 | } | 
| 89 | 92 | } | 
| @@ -63,7 +63,8 @@ | ||
| 63 | 63 | */ | 
| 64 | 64 | protected function limitString($value, $limit = 10, $end = '...') | 
| 65 | 65 |      { | 
| 66 | -        if (mb_strwidth($value, 'UTF-8') <= $limit) { | |
| 66 | + if (mb_strwidth($value, 'UTF-8') <= $limit) | |
| 67 | +        { | |
| 67 | 68 | return $value; | 
| 68 | 69 | } | 
| 69 | 70 | |
| @@ -54,7 +54,8 @@ discard block | ||
| 54 | 54 |      { | 
| 55 | 55 | $length = strlen($binaryMessage); | 
| 56 | 56 | |
| 57 | -        if ($length > self::PAYLOAD_MAX_BYTES) { | |
| 57 | + if ($length > self::PAYLOAD_MAX_BYTES) | |
| 58 | +        { | |
| 58 | 59 | throw new \InvalidArgumentException( | 
| 59 | 60 |                  sprintf('The maximum size allowed for a notification payload is %s bytes; Apple Push Notification Service refuses any notification that exceeds this limit.', self::PAYLOAD_MAX_BYTES) | 
| 60 | 61 | ); | 
| @@ -70,7 +71,8 @@ discard block | ||
| 70 | 71 | */ | 
| 71 | 72 | protected function getApnsResource() | 
| 72 | 73 |      { | 
| 73 | -        if (! is_resource($this->apnsResource)) { | |
| 74 | + if (! is_resource($this->apnsResource)) | |
| 75 | +        { | |
| 74 | 76 | $this->apnsResource = $this->createStreamClient(); | 
| 75 | 77 | } | 
| 76 | 78 | |
| @@ -114,7 +116,8 @@ discard block | ||
| 114 | 116 | */ | 
| 115 | 117 | public function close() | 
| 116 | 118 |      { | 
| 117 | -        if (is_resource($this->apnsResource)) { | |
| 119 | + if (is_resource($this->apnsResource)) | |
| 120 | +        { | |
| 118 | 121 | fclose($this->apnsResource); | 
| 119 | 122 | } | 
| 120 | 123 | } | 
| @@ -43,9 +43,12 @@ discard block | ||
| 43 | 43 | */ | 
| 44 | 44 | public function getP12Certificate() | 
| 45 | 45 |      { | 
| 46 | -        try { | |
| 46 | + try | |
| 47 | +        { | |
| 47 | 48 | return $this->storage->get($this->paths['p12']); | 
| 48 | -        } catch (\Exception $e) { | |
| 49 | + } | |
| 50 | + catch (\Exception $e) | |
| 51 | +        { | |
| 49 | 52 | return ''; | 
| 50 | 53 | } | 
| 51 | 54 | } | 
| @@ -57,9 +60,12 @@ discard block | ||
| 57 | 60 | */ | 
| 58 | 61 | public function getPassword() | 
| 59 | 62 |      { | 
| 60 | -        try { | |
| 63 | + try | |
| 64 | +        { | |
| 61 | 65 | return $this->storage->get($this->paths['password']); | 
| 62 | -        } catch (\Exception $e) { | |
| 66 | + } | |
| 67 | + catch (\Exception $e) | |
| 68 | +        { | |
| 63 | 69 | return ''; | 
| 64 | 70 | } | 
| 65 | 71 | } | 
| @@ -71,9 +77,12 @@ discard block | ||
| 71 | 77 | */ | 
| 72 | 78 | public function getPemCertificate() | 
| 73 | 79 |      { | 
| 74 | -        try { | |
| 80 | + try | |
| 81 | +        { | |
| 75 | 82 | return $this->storage->get($this->paths['pem']); | 
| 76 | -        } catch (\Exception $e) { | |
| 83 | + } | |
| 84 | + catch (\Exception $e) | |
| 85 | +        { | |
| 77 | 86 | return ''; | 
| 78 | 87 | } | 
| 79 | 88 | } | 
| @@ -31,7 +31,8 @@ discard block | ||
| 31 | 31 | */ | 
| 32 | 32 | protected static function initBrowser(array $config) | 
| 33 | 33 |      { | 
| 34 | -        if (static::$browser) { | |
| 34 | + if (static::$browser) | |
| 35 | +        { | |
| 35 | 36 | return; | 
| 36 | 37 | } | 
| 37 | 38 | |
| @@ -67,26 +68,34 @@ discard block | ||
| 67 | 68 | |
| 68 | 69 | $this->flush( | 
| 69 | 70 | $subscribers, | 
| 70 | -            function (Response $response, $index) use ($notification, $total) { | |
| 71 | -                try { | |
| 71 | + function (Response $response, $index) use ($notification, $total) | |
| 72 | +            { | |
| 73 | + try | |
| 74 | +                { | |
| 72 | 75 | $response = json_decode($response->getBody()); | 
| 73 | 76 | |
| 74 | -                    if (json_last_error() !== JSON_ERROR_NONE) { | |
| 77 | + if (json_last_error() !== JSON_ERROR_NONE) | |
| 78 | +                    { | |
| 75 | 79 | throw new \Exception(); | 
| 76 | 80 | } | 
| 77 | 81 | |
| 78 | -                    if ($response->success > 0) { | |
| 82 | + if ($response->success > 0) | |
| 83 | +                    { | |
| 79 | 84 | Driver::emit(new NotificationSent($notification, (int) $response->success)); | 
| 80 | 85 | } | 
| 81 | 86 | |
| 82 | -                    if ($response->failure > 0) { | |
| 87 | + if ($response->failure > 0) | |
| 88 | +                    { | |
| 83 | 89 | Driver::emit(new NotificationFailed($notification, (int) $response->failure)); | 
| 84 | 90 | } | 
| 85 | -                } catch (\Exception $e) { | |
| 91 | + } | |
| 92 | + catch (\Exception $e) | |
| 93 | +                { | |
| 86 | 94 | Driver::emit(new NotificationFailed($notification, $this->calculateChunkSize($index, $total))); | 
| 87 | 95 | } | 
| 88 | 96 | }, | 
| 89 | -            function ($reason, $index) use ($notification, $total) { | |
| 97 | + function ($reason, $index) use ($notification, $total) | |
| 98 | +            { | |
| 90 | 99 | Driver::emit(new NotificationFailed($notification, $this->calculateChunkSize($index, $total))); | 
| 91 | 100 | } | 
| 92 | 101 | ); | 
| @@ -104,7 +113,8 @@ discard block | ||
| 104 | 113 | $url = $this->config['url']; | 
| 105 | 114 | $headers = static::$headers; | 
| 106 | 115 | |
| 107 | -        foreach ($this->batch($subscribers) as $index => $chunk) { | |
| 116 | + foreach ($this->batch($subscribers) as $index => $chunk) | |
| 117 | +        { | |
| 108 | 118 | $content = $this->getRequestContent($chunk); | 
| 109 | 119 | $headers['Content-Length'] = strlen($content); | 
| 110 | 120 | |
| @@ -127,11 +137,16 @@ discard block | ||
| 127 | 137 | $offset = ($index + 1) * $chunkSize; | 
| 128 | 138 | $chunks = ceil($total / $chunkSize); | 
| 129 | 139 | |
| 130 | -        if ($offset <= $total) { | |
| 140 | + if ($offset <= $total) | |
| 141 | +        { | |
| 131 | 142 | $return = $chunkSize; | 
| 132 | -        } elseif ($offset > $total + $chunkSize) { | |
| 143 | + } | |
| 144 | + elseif ($offset > $total + $chunkSize) | |
| 145 | +        { | |
| 133 | 146 | $return = 0; | 
| 134 | -        } else { | |
| 147 | + } | |
| 148 | + else | |
| 149 | +        { | |
| 135 | 150 | $return = $total - ($chunks - 1) * $chunkSize; | 
| 136 | 151 | } | 
| 137 | 152 | |
| @@ -149,7 +164,8 @@ discard block | ||
| 149 | 164 | $tokens = []; | 
| 150 | 165 | |
| 151 | 166 | /** @var Subscriber $subscriber */ | 
| 152 | -        foreach ($subscribers as $subscriber) { | |
| 167 | + foreach ($subscribers as $subscriber) | |
| 168 | +        { | |
| 153 | 169 | $tokens[] = $subscriber->getToken(); | 
| 154 | 170 | } | 
| 155 | 171 | |
| @@ -31,7 +31,8 @@ discard block | ||
| 31 | 31 | */ | 
| 32 | 32 | protected static function initBrowser(array $config) | 
| 33 | 33 |      { | 
| 34 | -        if (static::$browser) { | |
| 34 | + if (static::$browser) | |
| 35 | +        { | |
| 35 | 36 | return; | 
| 36 | 37 | } | 
| 37 | 38 | |
| @@ -65,12 +66,16 @@ discard block | ||
| 65 | 66 | |
| 66 | 67 | $this->flush( | 
| 67 | 68 | $subscribers, | 
| 68 | -            function (Response $response, $index) use ($notification) { | |
| 69 | -                try { | |
| 70 | -                    if ($response->getStatusCode() != 201) { | |
| 69 | + function (Response $response, $index) use ($notification) | |
| 70 | +            { | |
| 71 | + try | |
| 72 | +                { | |
| 73 | + if ($response->getStatusCode() != 201) | |
| 74 | +                    { | |
| 71 | 75 | $response = json_decode($response->getBody()); | 
| 72 | 76 | |
| 73 | -                        if (json_last_error() !== JSON_ERROR_NONE) { | |
| 77 | + if (json_last_error() !== JSON_ERROR_NONE) | |
| 78 | +                        { | |
| 74 | 79 |                              throw new \Exception('Firefox: bad json'); | 
| 75 | 80 | } | 
| 76 | 81 | |
| @@ -78,11 +83,14 @@ discard block | ||
| 78 | 83 | } | 
| 79 | 84 | |
| 80 | 85 | Driver::emit(new NotificationSent($notification)); | 
| 81 | -                } catch (\Exception $e) { | |
| 86 | + } | |
| 87 | + catch (\Exception $e) | |
| 88 | +                { | |
| 82 | 89 | Driver::emit(new NotificationFailed($notification)); | 
| 83 | 90 | } | 
| 84 | 91 | }, | 
| 85 | -            function ($reason, $index) use ($notification) { | |
| 92 | + function ($reason, $index) use ($notification) | |
| 93 | +            { | |
| 86 | 94 | Driver::emit(new NotificationFailed($notification)); | 
| 87 | 95 | } | 
| 88 | 96 | ); | 
| @@ -100,7 +108,8 @@ discard block | ||
| 100 | 108 | $headers = static::$headers; | 
| 101 | 109 | $content = ''; | 
| 102 | 110 | |
| 103 | -        foreach ($subscribers as $subscriber) { | |
| 111 | + foreach ($subscribers as $subscriber) | |
| 112 | +        { | |
| 104 | 113 | $headers['Content-Length'] = strlen($content); | 
| 105 | 114 | |
| 106 | 115 |              yield new Request('POST', $this->getUrl($subscriber), $headers, $content); | 
| @@ -74,11 +74,17 @@ | ||
| 74 | 74 | */ | 
| 75 | 75 | protected function flush(array $subscribers, \Closure $success = null, \Closure $fail = null) | 
| 76 | 76 |      { | 
| 77 | -        if (is_null($success)) { | |
| 78 | -            $success = function ($response, $index) {}; | |
| 77 | + if (is_null($success)) | |
| 78 | +        { | |
| 79 | + $success = function ($response, $index) | |
| 80 | +            { | |
| 81 | +}; | |
| 79 | 82 | } | 
| 80 | -        if (is_null($fail)) { | |
| 81 | -            $fail = function ($reason, $index) {}; | |
| 83 | + if (is_null($fail)) | |
| 84 | +        { | |
| 85 | + $fail = function ($reason, $index) | |
| 86 | +            { | |
| 87 | +}; | |
| 82 | 88 | } | 
| 83 | 89 | |
| 84 | 90 | $pool = new Pool(static::$browser, $this->prepareRequests($subscribers), [ |