PersistCollectionToFile::load()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 13
cts 13
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 6
nop 0
crap 4
1
<?php
2
3
namespace TonicHealthCheck\Check\Email\Persist;
4
5
use TonicHealthCheck\Check\Email\Entity\EmailSendReceiveCollection;
6
7
/**
8
 * Class PersistCollectionToFile
9
 */
10
class PersistCollectionToFile implements PersistCollectionInterface
11
{
12
    const DATA_FILE_NAME = 'email_send_receive_collection.data';
13
14
    /**
15
     * @var string;
16
     */
17
    private $savePath;
18
19
    /**
20
     * @var string;
21
     */
22
    private $saveFileName = self::DATA_FILE_NAME;
23
24
    /**
25
     * @var EmailSendReceiveCollection;
26
     */
27
    private $emailSendReceiveC;
28
29
30
    /**
31
     * PersistCollectionToFile constructor.
32
     * @param string      $savePath
33
     * @param null|string $saveFileName
34
     * @throws PersistCollectionToFileException
35
     */
36 17
    public function __construct($savePath = null, $saveFileName = null)
37
    {
38 17
        if (null === $savePath) {
39 1
            $savePath = sys_get_temp_dir();
40 1
        }
41 17
        $this->setSavePath($savePath);
42
43 16
        if (null !== $saveFileName) {
44 2
            $this->setSaveFileName($saveFileName);
45 2
        }
46 16
    }
47
48
    /**
49
     * PersistCollectionToFiledestruct.
50
     */
51 4
    public function __destruct()
52
    {
53 4
        $this->flush();
54 4
    }
55
56
    /**
57
     * @param EmailSendReceiveCollection $emailSendReceiveC
58
     */
59 14
    public function persist(EmailSendReceiveCollection $emailSendReceiveC)
60
    {
61 14
        $this->setEmailSendReceiveC($emailSendReceiveC);
62 14
    }
63
64
    /**
65
     * @return bool
66
     */
67 15
    public function flush()
68
    {
69 15
        file_put_contents(
70 15
            $this->getDataFilePath(),
71 15
            serialize($this->getEmailSendReceiveC())
72 15
        );
73 15
    }
74
75
    /**
76
     * @return null|EmailSendReceiveCollection
77
     */
78 14
    public function load()
79
    {
80
81
82 14
        if (is_readable($this->getDataFilePath())) {
83 13
            $emailSendReceiveC = unserialize(
84 13
                file_get_contents($this->getDataFilePath())
85 13
            );
86
87 13
            if ($emailSendReceiveC instanceof EmailSendReceiveCollection) {
88 13
                $this->persist($emailSendReceiveC);
89 13
            }
90 13
        }
91
92 14
        if (null === $this->getEmailSendReceiveC()) {
93 2
            $this->persist(new EmailSendReceiveCollection());
94 2
        }
95
96 14
        return $this->getEmailSendReceiveC();
97
    }
98
99
    /**
100
     * @return bool
101
     */
102 2
    public function delete()
103
    {
104 2
        if (is_writable($this->getDataFilePath())) {
105 1
            $returnFlag = unlink($this->getDataFilePath());
106 1
        } else {
107 2
            $returnFlag = false;
108
        }
109
110 2
        return $returnFlag;
111
    }
112
113
114
115
    /**
116
     * @return string
117
     */
118 15
    protected function getSavePath()
119
    {
120 15
        return $this->savePath;
121
    }
122
123
    /**
124
     * @param string $savePath
125
     * @throws PersistCollectionToFileException
126
     */
127 17
    protected function setSavePath($savePath)
128
    {
129 17
        if (!is_writable($savePath)) {
130 1
            throw PersistCollectionToFileException::dirForSaveDoesNotWritable($savePath);
131
        }
132 16
        $this->savePath = rtrim($savePath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
133 16
    }
134
135
    /**
136
     * @return EmailSendReceiveCollection
137
     */
138 15
    protected function getEmailSendReceiveC()
139
    {
140 15
        return $this->emailSendReceiveC;
141
    }
142
143
    /**
144
     * @param EmailSendReceiveCollection $emailSendReceiveC
145
     */
146 14
    protected function setEmailSendReceiveC(EmailSendReceiveCollection $emailSendReceiveC)
147
    {
148 14
        $this->emailSendReceiveC = $emailSendReceiveC;
149 14
    }
150
151
    /**
152
     * @return string
153
     */
154 15
    public function getDataFilePath()
155
    {
156 15
        return $this->getSavePath().$this->getSaveFileName();
157
    }
158
159
    /**
160
     * @return string
161
     */
162 15
    protected function getSaveFileName()
163
    {
164 15
        return $this->saveFileName;
165
    }
166
167
    /**
168
     * @param string $saveFileName
169
     */
170 2
    protected function setSaveFileName($saveFileName)
171
    {
172 2
        $this->saveFileName = $saveFileName;
173 2
    }
174
}
175