FileCertificateStorage::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Bunq\Certificate\Storage;
4
5
use Bunq\Certificate\Certificate;
6
use Bunq\Certificate\CertificateType;
7
use Bunq\Certificate\DefaultCertificate;
8
9 View Code Duplication
final class FileCertificateStorage implements CertificateStorage
0 ignored issues
show
Duplication introduced by
This class 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...
10
{
11
    /**
12
     * @var string
13
     */
14
    private $path;
15
16
    /**
17
     * @var array
18
     */
19
    private $cache = [];
20
21
    /**
22
     * @param string $path
23
     */
24
    public function __construct($path)
25
    {
26
        $this->path = (string)$path . '/certificates';
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function load(CertificateType $certificateType)
33
    {
34
        if (!file_exists($this->path . '/' . $certificateType->toString())) {
35
            throw new CertificateNotFoundException($certificateType, $this->path);
36
        }
37
38
        $certificate = $this->loadCertificate($certificateType);
39
40
        return DefaultCertificate::fromString($certificate);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function save(Certificate $certificate, CertificateType $certificateType)
47
    {
48
        $certificate = trim($certificate->toString());
49
50
        if (!file_exists($this->path)) {
51
            mkdir($this->path);
52
        }
53
54
        file_put_contents($this->path . '/' . $certificateType, $certificate);
55
56
        // save server
57
        $this->cache[$certificateType->toString()] = $certificate;
58
    }
59
60
    /**
61
     * @param CertificateType $certificateType
62
     *
63
     * @return string
64
     *
65
     * @throws CertificateNotFoundException
66
     */
67
    private function loadCertificate(CertificateType $certificateType)
68
    {
69
        // take from cache, filesystems are slow
70
        if (isset($this->cache[$certificateType->toString()])) {
71
            return $this->cache[$certificateType->toString()];
72
        }
73
74
        // load from file system
75
        $certificate = file_get_contents($this->path . '/' . $certificateType->toString());
76
77
        if (!$certificate) {
78
            throw new CertificateNotFoundException($certificateType, $this->path);
79
        }
80
81
        // save the result in the cache
82
        $this->cache[$certificateType->toString()] = $certificate;
83
84
        return $certificate;
85
    }
86
}
87