LocalFile::saveAccessToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php namespace Crunch\Salesforce\TokenStore;
2
3
use Crunch\Salesforce\AccessToken;
4
use Crunch\Salesforce\AccessTokenGenerator;
5
6
class LocalFile implements StoreInterface
7
{
8
9
    /**
10
     * @var AccessTokenGenerator
11
     */
12
    private $accessTokenGenerator;
13
14
    /**
15
     * @var string
16
     */
17
    private $filePath;
18
19
    /**
20
     * @var string
21
     */
22
    private $fileName = 'sf-key';
23
24
    /**
25
     * @var LocalFileConfigInterface
26
     */
27
    private $config;
28
29
30
    /**
31
     * @param AccessTokenGenerator     $accessTokenGenerator
32
     * @param LocalFileConfigInterface $config
33
     */
34
    public function __construct(AccessTokenGenerator $accessTokenGenerator, LocalFileConfigInterface $config)
35
    {
36
        $this->accessTokenGenerator = $accessTokenGenerator;
37
        $this->filePath             = $config->getFilePath();
38
        $this->config               = $config;
39
    }
40
41
    /**
42
     * @return AccessToken
43
     * @throws \Exception
44
     */
45
    public function fetchAccessToken()
46
    {
47
        try {
48
            $accessTokenJson = file_get_contents($this->filePath . '/' . $this->fileName);
49
        } catch (\ErrorException $e) {
50
            throw new \Exception('Salesforce access token not found');
51
        }
52
53
        return $this->accessTokenGenerator->createFromJson($accessTokenJson);
54
    }
55
56
    /**
57
     * @param AccessToken $accessToken
58
     */
59
    public function saveAccessToken(AccessToken $accessToken)
60
    {
61
        file_put_contents($this->filePath . '/' . $this->fileName, $accessToken->toJson());
62
    }
63
}
64