Completed
Pull Request — master (#101)
by Chimit
01:26
created

Certificate   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 21.05 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 20
loc 95
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 4 1
A authenticateClient() 0 13 1
A generateApnsTopic() 20 20 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the Pushok package.
5
 *
6
 * (c) Arthur Edamov <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Pushok\AuthProvider;
13
14
use Pushok\AuthProviderInterface;
15
use Pushok\Request;
16
17
/**
18
 * Class Certificate
19
 * @package Pushok\AuthProvider
20
 *
21
 * @see     http://bit.ly/communicating-with-apns
22
 */
23
class Certificate implements AuthProviderInterface
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
24
{
25
    /**
26
     * Path to certificate.
27
     *
28
     * @var string
29
     */
30
    private $certificatePath;
31
32
    /**
33
     * Certificate secret.
34
     *
35
     * @var string
36
     */
37
    private $certificateSecret;
38
39
    /**
40
     * The bundle ID for app obtained from Apple developer account.
41
     *
42
     * @var string
43
     */
44
    private $appBundleId;
45
46
    /**
47
     * This provider accepts the following options:
48
     *
49
     * - certificate_path
50
     * - certificate_secret
51
     *
52
     * @param array $options
53
     */
54
    private function __construct(array $options)
55
    {
56
        $this->certificatePath   = $options['certificate_path'] ;
57
        $this->certificateSecret = $options['certificate_secret'];
58
        $this->appBundleId       = $options['app_bundle_id'] ?? null;
59
    }
60
61
    /**
62
     * Create Certificate Auth provider.
63
     *
64
     * @param array $options
65
     * @return Certificate
66
     */
67
    public static function create(array $options): Certificate
68
    {
69
        return new self($options);
70
    }
71
72
    /**
73
     * Authenticate client.
74
     *
75
     * @param Request $request
76
     */
77
    public function authenticateClient(Request $request)
78
    {
79
        $request->addOptions(
80
            [
81
                CURLOPT_SSLCERT        => $this->certificatePath,
82
                CURLOPT_SSLCERTPASSWD  => $this->certificateSecret,
83
                CURLOPT_SSL_VERIFYPEER => true
84
            ]
85
        );
86
        $request->addHeaders([
87
            'apns-topic' => $this->generateApnsTopic($request->getHeaders()['apns-push-type']),
88
        ]);
89
    }
90
91
    /**
92
     * Generate a correct apns-topic string
93
     *
94
     * @param string $pushType
95
     * @return string
96
     */
97 View Code Duplication
    public function generateApnsTopic(string $pushType)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        switch ($pushType) {
100
            case 'voip':
101
                return $this->appBundleId . '.voip';
102
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
103
104
            case 'complication':
105
                return $this->appBundleId . '.complication';
106
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
107
108
            case 'fileprovider':
109
                return $this->appBundleId . '.pushkit.fileprovider';
110
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
111
112
            default:
113
                return $this->appBundleId;
114
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
115
        }
116
    }
117
}
118