1 | <?php |
||
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() |
|
55 | |||
56 | /** |
||
57 | * @param EmailSendReceiveCollection $emailSendReceiveC |
||
58 | */ |
||
59 | 14 | public function persist(EmailSendReceiveCollection $emailSendReceiveC) |
|
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() |
|
122 | |||
123 | /** |
||
124 | * @param string $savePath |
||
125 | * @throws PersistCollectionToFileException |
||
126 | */ |
||
127 | 17 | protected function setSavePath($savePath) |
|
134 | |||
135 | /** |
||
136 | * @return EmailSendReceiveCollection |
||
137 | */ |
||
138 | 15 | protected function getEmailSendReceiveC() |
|
142 | |||
143 | /** |
||
144 | * @param EmailSendReceiveCollection $emailSendReceiveC |
||
145 | */ |
||
146 | 14 | protected function setEmailSendReceiveC(EmailSendReceiveCollection $emailSendReceiveC) |
|
150 | |||
151 | /** |
||
152 | * @return string |
||
153 | */ |
||
154 | 15 | public function getDataFilePath() |
|
158 | |||
159 | /** |
||
160 | * @return string |
||
161 | */ |
||
162 | 15 | protected function getSaveFileName() |
|
166 | |||
167 | /** |
||
168 | * @param string $saveFileName |
||
169 | */ |
||
170 | 2 | protected function setSaveFileName($saveFileName) |
|
174 | } |
||
175 |