Passed
Push — master ( 95d545...35bca5 )
by Dmitry
04:09
created

PersistCollectionToFile::getSaveFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 16
    public function __construct($savePath, $saveFileName = null)
37
    {
38 16
        $this->setSavePath($savePath);
39
40 15
        if (null !== $saveFileName) {
41 2
            $this->setSaveFileName($saveFileName);
42 2
        }
43 15
    }
44
45
    /**
46
     * PersistCollectionToFiledestruct.
47
     */
48 3
    public function __destruct()
49
    {
50 3
        $this->flush();
51 3
    }
52
53
    /**
54
     * @param EmailSendReceiveCollection $emailSendReceiveC
55
     */
56 14
    public function persist(EmailSendReceiveCollection $emailSendReceiveC)
57
    {
58 14
        $this->setEmailSendReceiveC($emailSendReceiveC);
59 14
    }
60
61
    /**
62
     * @return bool
63
     */
64 14
    public function flush()
65
    {
66 14
        file_put_contents(
67 14
            $this->getDataFilePath(),
68 14
            serialize($this->getEmailSendReceiveC())
69 14
        );
70 14
    }
71
72
    /**
73
     * @return null|EmailSendReceiveCollection
74
     */
75 14
    public function load()
76
    {
77
78
79 14
        if (is_readable($this->getDataFilePath())) {
80 13
            $emailSendReceiveC = unserialize(
81 13
                file_get_contents($this->getDataFilePath())
82 13
            );
83
84 13
            if ($emailSendReceiveC instanceof EmailSendReceiveCollection) {
85 13
                $this->persist($emailSendReceiveC);
86 13
            }
87 13
        }
88
89 14
        if (null === $this->getEmailSendReceiveC()) {
90 2
            $this->persist(new EmailSendReceiveCollection());
91 2
        }
92
93 14
        return $this->getEmailSendReceiveC();
94
    }
95
96
    /**
97
     * @return bool
98
     */
99 2
    public function delete()
100
    {
101 2
        if (is_writable($this->getDataFilePath())) {
102 1
            $returnFlag = unlink($this->getDataFilePath());
103 1
        } else {
104 2
            $returnFlag = false;
105
        }
106
107 2
        return $returnFlag;
108
    }
109
110
111
112
    /**
113
     * @return string
114
     */
115 14
    protected function getSavePath()
116
    {
117 14
        return $this->savePath;
118
    }
119
120
    /**
121
     * @param string $savePath
122
     * @throws PersistCollectionToFileException
123
     */
124 16
    protected function setSavePath($savePath)
125
    {
126 16
        if (!is_writable($savePath)) {
127 1
            throw PersistCollectionToFileException::dirForSaveDoesNotWritable($savePath);
128
        }
129 15
        $this->savePath = rtrim($savePath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
130 15
    }
131
132
    /**
133
     * @return EmailSendReceiveCollection
134
     */
135 14
    protected function getEmailSendReceiveC()
136
    {
137 14
        return $this->emailSendReceiveC;
138
    }
139
140
    /**
141
     * @param EmailSendReceiveCollection $emailSendReceiveC
142
     */
143 14
    protected function setEmailSendReceiveC(EmailSendReceiveCollection $emailSendReceiveC)
144
    {
145 14
        $this->emailSendReceiveC = $emailSendReceiveC;
146 14
    }
147
148
    /**
149
     * @return string
150
     */
151 14
    public function getDataFilePath()
152
    {
153 14
        return $this->getSavePath().$this->getSaveFileName();
154
    }
155
156
    /**
157
     * @return string
158
     */
159 14
    protected function getSaveFileName()
160
    {
161 14
        return $this->saveFileName;
162
    }
163
164
    /**
165
     * @param string $saveFileName
166
     */
167 2
    protected function setSaveFileName($saveFileName)
168
    {
169 2
        $this->saveFileName = $saveFileName;
170 2
    }
171
}
172