LocalFileTokenManager::loadTokenInfo()   C
last analyzed

Complexity

Conditions 7
Paths 13

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 13
nop 0
dl 0
loc 23
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/*
3
 * The MIT License
4
 *
5
 * Copyright 2016 BCL Technologies.
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
26
namespace Bcl\EasyPdfCloud;
27
28
use Exception;
29
use function rtrim;
30
use function sys_get_temp_dir;
31
use function mkdir;
32
use function is_dir;
33
use function is_file;
34
use function file_get_contents;
35
use function file_put_contents;
36
use function unserialize;
37
use function serialize;
38
use function unlink;
39
40
class LocalFileTokenManager implements IOAuth2TokenManager
41
{
42
    private $tokenInfo;
43
    private $filePath;
44
    private $lockFilePath;
45
46
    public function __construct($clientId)
47
    {
48
        $tempDir = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR);
49
50
        $tempFileDir = $tempDir;
51
        $tempFileDir .= DIRECTORY_SEPARATOR . 'easyPdfCloud';
52
        $tempFileDir .= DIRECTORY_SEPARATOR . 'clients';
53
        $tempFileDir .= DIRECTORY_SEPARATOR . $clientId;
54
55
        if (false === is_dir($tempFileDir)) {
56
            mkdir($tempFileDir, 0755, true);
57
        }
58
59
        $this->filePath = $tempFileDir . DIRECTORY_SEPARATOR . 'token.serialized';
60
        $this->lockFilePath = $tempFileDir . DIRECTORY_SEPARATOR . 'token.lock';
61
    }
62
63
    public function loadTokenInfo()
64
    {
65
        if (null === $this->tokenInfo && is_file($this->filePath)) {
66
            $lock = new FileLock($this->lockFilePath);
67
68
            try {
69
                if (null === $this->tokenInfo && is_file($this->filePath)) {
70
                    $serialized = @file_get_contents($this->filePath);
71
                    if (false !== $serialized) {
72
                        $this->tokenInfo = unserialize($serialized);
73
                    }
74
                }
75
            } catch (Exception $e) {
76
                unlink($this->filePath);
77
                throw $e;
78
            } finally {
79
                $lock->unlock();
80
                unset($lock);
81
            }
82
83
        }
84
85
        return $this->tokenInfo;
86
    }
87
88
    public function saveTokenInfo(array $tokenInfo)
89
    {
90
        $this->tokenInfo = $tokenInfo;
91
92
        $lock = new FileLock($this->lockFilePath);
93
94
        try {
95
            $serialized = serialize($tokenInfo);
96
            file_put_contents($this->filePath, $serialized);
97
        } catch (Exception $e) {
98
            throw $e;
99
        } finally {
100
            $lock->unlock();
101
            unset($lock);
102
        }
103
    }
104
}
105