Completed
Push — master ( 10c8f9...49b9c4 )
by Semyon
01:41
created

CouldNotSendNotification::getResponses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace SemyonChetvertnyh\ApnNotificationChannel\Exceptions;
4
5
use Exception;
6
use Pushok\ApnsResponseInterface;
7
use SemyonChetvertnyh\ApnNotificationChannel\ApnsResponseCollection;
8
9
class CouldNotSendNotification extends Exception
10
{
11
    /**
12
     * @var \SemyonChetvertnyh\ApnNotificationChannel\ApnsResponseCollection|\Pushok\ApnsResponseInterface[]
13
     */
14
    protected $responses;
15
16
    /**
17
     * Create an instance of exception attaching responses.
18
     *
19
     * @param  \SemyonChetvertnyh\ApnNotificationChannel\ApnsResponseCollection|\Pushok\ApnsResponseInterface[]  $responses
20
     * @return $this
21
     */
22
    public static function withUnsuccessful(ApnsResponseCollection $responses)
23
    {
24
        $message = $responses->map(function (ApnsResponseInterface $response) {
25
            return $response->getErrorDescription()
26
                ?? $response->getReasonPhrase()
27
                ?? $response->getErrorReason();
28
        })->unique()->implode('; ');
29
30
        return (new static($message))
31
            ->setResponses($responses);
32
    }
33
34
    /**
35
     * Attach the responses.
36
     *
37
     * @param  \SemyonChetvertnyh\ApnNotificationChannel\ApnsResponseCollection|\Pushok\ApnsResponseInterface[]  $responses
38
     * @return $this
39
     */
40
    protected function setResponses(ApnsResponseCollection $responses)
41
    {
42
        $this->responses = $responses;
43
44
        return $this;
45
    }
46
47
    /**
48
     * Get the responses collection.
49
     *
50
     * @return \SemyonChetvertnyh\ApnNotificationChannel\ApnsResponseCollection|\Pushok\ApnsResponseInterface[]
51
     */
52
    public function getResponses()
53
    {
54
        return $this->responses;
55
    }
56
57
    /**
58
     * Get a first response.
59
     *
60
     * @return \Pushok\ApnsResponseInterface
61
     * @deprecated Use getResponses() instead.
62
     */
63
    public function getResponse()
64
    {
65
        return $this->responses[0] ?? null;
66
    }
67
}
68