Completed
Push — master ( 613542...020c5c )
by Julián
03:39
created

ApnsClientBuilder::buildPush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
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\Service\Client;
11
12
use Jgut\Tify\Exception\ServiceException;
13
use ZendService\Apple\Apns\Client\AbstractClient;
14
use ZendService\Apple\Apns\Client\Feedback as FeedbackClient;
15
use ZendService\Apple\Apns\Client\Message as MessageClient;
16
17
/**
18
 * Class ApnsClientBuilder
19
 */
20
class ApnsClientBuilder
21
{
22
    /**
23
     * Get opened push service client.
24
     *
25
     * @param string $certificate
26
     * @param string $passPhrase
27
     * @param bool   $sandbox
28
     *
29
     * @throws \Jgut\Tify\Exception\ServiceException
30
     *
31
     * @return \ZendService\Apple\Apns\Client\Message
32
     */
33
    public static function buildPush($certificate, $passPhrase = '', $sandbox = false)
34
    {
35
        return static::buildClient(new MessageClient, $certificate, $passPhrase, $sandbox);
36
    }
37
38
    /**
39
     * Get opened feedback service client.
40
     *
41
     * @param string $certificate
42
     * @param string $passPhrase
43
     * @param bool   $sandbox
44
     *
45
     * @return \ZendService\Apple\Apns\Client\Feedback
46
     */
47
    public static function buildFeedback($certificate, $passPhrase = '', $sandbox = false)
48
    {
49
        return static::buildClient(new FeedbackClient, $certificate, $passPhrase, $sandbox);
50
    }
51
52
    /**
53
     * Get opened client.
54
     *
55
     * @param \ZendService\Apple\Apns\Client\AbstractClient $client
56
     * @param string                                        $certificate
57
     * @param string                                        $passPhrase
58
     * @param bool                                          $sandbox
59
     *
60
     * @return \ZendService\Apple\Apns\Client\AbstractClient
61
     *
62
     * @codeCoverageIgnore
63
     */
64
    protected static function buildClient(AbstractClient $client, $certificate, $passPhrase = '', $sandbox = false)
65
    {
66
        try {
67
            $client->open(
68
                (bool) $sandbox ? AbstractClient::SANDBOX_URI : AbstractClient::PRODUCTION_URI,
69
                $certificate,
70
                $passPhrase
71
            );
72
        } catch (\Exception $exception) {
73
            throw new ServiceException($exception->getMessage(), $exception->getCode(), $exception);
74
        }
75
76
        return $client;
77
    }
78
}
79