ContentCertificate::__destruct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the AppleApnPush package
7
 *
8
 * (c) Vitaliy Zhuk <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code
12
 */
13
14
namespace Apple\ApnPush\Certificate;
15
16
/**
17
 * Certificate with content. You can use this certificate, if you save content to database
18
 * or another storage. Before get certificate file, this system create a temporary file certificate
19
 * and returns path to it. After close connection (destruct), the temporary file
20
 * will be removed.
21
 */
22
class ContentCertificate implements CertificateInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    private $content;
28
29
    /**
30
     * @var string
31
     */
32
    private $passPhrase;
33
34
    /**
35
     * @var string
36
     */
37
    private $tmpDir;
38
39
    /**
40
     * @var string
41
     */
42
    private $certificateFilePath;
43
44
    /**
45
     * Construct
46
     *
47
     * @param string $content
48
     * @param string $passPhrase
49
     * @param string $tmpDir
50
     */
51
    public function __construct(string $content, string $passPhrase, string $tmpDir)
52
    {
53
        $this->content = $content;
54
        $this->passPhrase = $passPhrase;
55
        $this->tmpDir = $tmpDir;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 View Code Duplication
    public function getPath(): string
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...
62
    {
63
        if ($this->certificateFilePath) {
64
            $this->removeTemporaryFile($this->certificateFilePath);
65
        }
66
67
        $this->certificateFilePath = $this->createTemporaryFile();
68
        file_put_contents($this->certificateFilePath, $this->content);
69
70
        return $this->certificateFilePath;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getPassPhrase(): string
77
    {
78
        return $this->passPhrase;
79
    }
80
81
    /**
82
     * Implement __destruct
83
     * Remove temporary file
84
     */
85
    public function __destruct()
86
    {
87
        if ($this->certificateFilePath) {
88
            $this->removeTemporaryFile($this->certificateFilePath);
89
        }
90
    }
91
92
    /**
93
     * Create a temporary file
94
     *
95
     * @return string Path to temporary file
96
     *
97
     * @throws \RuntimeException
98
     */
99 View Code Duplication
    private function createTemporaryFile(): string
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...
100
    {
101
        $tmpDir = $this->tmpDir;
102
103
        $tmpFileName = \md5(\uniqid((string) \mt_rand(), true)).'.pem';
104
105
        $tmpFilePath = $tmpDir.'/'.$tmpFileName;
106
107
        $errorCode = $errorMessage = null;
108
109
        \set_error_handler(function ($errCode, $errMessage) use (&$errorCode, &$errorMessage) {
110
            $errorCode = $errCode;
111
            $errorMessage = $errMessage;
112
        });
113
114
        if (!\file_exists($tmpDir)) {
115
            \mkdir($tmpDir, 0600, true);
116
117
            if ($errorCode || $errorMessage) {
118
                \restore_error_handler();
119
120
                // Error create directory
121
                throw new \RuntimeException(sprintf(
122
                    'Can not create temporary directory "%s". Error: %s [%d].',
123
                    $tmpDir,
124
                    $errorMessage ?: 'Undefined',
125
                    $errorCode ?: '0'
126
                ));
127
            }
128
        }
129
130
        \touch($tmpFilePath);
131
132
        if ($errorCode || $errorMessage) {
133
            \restore_error_handler();
134
135
            // Error create file
136
            throw new \RuntimeException(sprintf(
137
                'Can not create temporary certificate file "%s". Error: %s [%d].',
138
                $tmpFilePath,
139
                $errorMessage ?: 'Undefined',
140
                $errorCode ?: '0'
141
            ));
142
        }
143
144
        \restore_error_handler();
145
146
        return $tmpFilePath;
147
    }
148
149
    /**
150
     * Remove temporary file
151
     *
152
     * @param string $filePath
153
     */
154
    private function removeTemporaryFile($filePath): void
155
    {
156
        // Set custom error handler for suppress error
157
        \set_error_handler(function () {
158
        });
159
160
        \unlink($filePath);
161
162
        \restore_error_handler();
163
    }
164
}
165