|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Contains CachePreserver class. |
|
5
|
|
|
* |
|
6
|
|
|
* PHP version 7.0+ |
|
7
|
|
|
* |
|
8
|
|
|
* LICENSE: |
|
9
|
|
|
* This file is part of Yet Another Php Eve Api Library also know as Yapeal |
|
10
|
|
|
* which can be used to access the Eve Online API data and place it into a |
|
11
|
|
|
* database. |
|
12
|
|
|
* Copyright (C) 2014-2017 Michael Cummings |
|
13
|
|
|
* |
|
14
|
|
|
* This program is free software: you can redistribute it and/or modify it |
|
15
|
|
|
* under the terms of the GNU Lesser General Public License as published by the |
|
16
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
|
17
|
|
|
* option) any later version. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
20
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
|
22
|
|
|
* for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
25
|
|
|
* along with this program. If not, see |
|
26
|
|
|
* <http://spdx.org/licenses/LGPL-3.0.html>. |
|
27
|
|
|
* |
|
28
|
|
|
* You should be able to find a copy of this license in the COPYING-LESSER.md |
|
29
|
|
|
* file. A copy of the GNU GPL should also be available in the COPYING.md file. |
|
30
|
|
|
* |
|
31
|
|
|
* @copyright 2014-2017 Michael Cummings |
|
32
|
|
|
* @license LGPL-3.0+ |
|
33
|
|
|
* @author Michael Cummings <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
namespace Yapeal\FileSystem; |
|
36
|
|
|
|
|
37
|
|
|
use Yapeal\Event\EveApiEventEmitterTrait; |
|
38
|
|
|
use Yapeal\Event\EveApiEventInterface; |
|
39
|
|
|
use Yapeal\Event\EveApiPreserverInterface; |
|
40
|
|
|
use Yapeal\Event\MediatorInterface; |
|
41
|
|
|
use Yapeal\Log\Logger; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Class CachePreserver |
|
45
|
|
|
*/ |
|
46
|
|
|
class CachePreserver implements EveApiPreserverInterface |
|
47
|
|
|
{ |
|
48
|
|
|
use SafeFileHandlingTrait; |
|
49
|
|
|
use EveApiEventEmitterTrait; |
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $cachePath |
|
52
|
|
|
* @param bool $preserve |
|
53
|
|
|
*/ |
|
54
|
8 |
|
public function __construct(string $cachePath, bool $preserve = false) |
|
55
|
|
|
{ |
|
56
|
8 |
|
$this->setCachePath($cachePath) |
|
57
|
8 |
|
->setPreserve($preserve); |
|
58
|
|
|
} |
|
59
|
|
|
/** |
|
60
|
|
|
* @param EveApiEventInterface $event |
|
61
|
|
|
* @param string $eventName |
|
62
|
|
|
* @param MediatorInterface $yem |
|
63
|
|
|
* |
|
64
|
|
|
* @return EveApiEventInterface |
|
65
|
|
|
* @throws \DomainException |
|
66
|
|
|
* @throws \InvalidArgumentException |
|
67
|
|
|
* @throws \LogicException |
|
68
|
|
|
* @throws \UnexpectedValueException |
|
69
|
|
|
*/ |
|
70
|
6 |
|
public function preserveEveApi( |
|
71
|
|
|
EveApiEventInterface $event, |
|
72
|
|
|
string $eventName, |
|
73
|
|
|
MediatorInterface $yem |
|
74
|
|
|
): EveApiEventInterface { |
|
75
|
6 |
|
if (!$this->shouldPreserve()) { |
|
76
|
1 |
|
return $event; |
|
77
|
|
|
} |
|
78
|
5 |
|
$data = $event->getData(); |
|
79
|
5 |
|
$yem->triggerLogEvent('Yapeal.Log.log', |
|
80
|
5 |
|
Logger::DEBUG, |
|
81
|
5 |
|
$this->getReceivedEventMessage($data, $eventName, __CLASS__)); |
|
82
|
|
|
// BaseSection/ApiHash.xml |
|
83
|
5 |
|
$cacheFile = sprintf('%s%s/%s%s.xml', |
|
84
|
5 |
|
$this->getCachePath(), |
|
85
|
4 |
|
ucfirst($data->getEveApiSectionName()), |
|
86
|
4 |
|
ucfirst($data->getEveApiName()), |
|
87
|
4 |
|
$data->getHash()); |
|
88
|
4 |
|
if ('' === $xml = $data->getEveApiXml()) { |
|
89
|
2 |
|
return $event; |
|
90
|
|
|
} |
|
91
|
|
|
// Insures retriever never see partly written file by deleting old file and using temp file for writing. |
|
92
|
2 |
|
if (false === $this->safeFileWrite($cacheFile, $xml)) { |
|
93
|
|
|
return $event; |
|
94
|
|
|
} |
|
95
|
2 |
|
return $event->setHandledSufficiently(); |
|
96
|
|
|
} |
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $value |
|
99
|
|
|
* |
|
100
|
|
|
* @return self Fluent interface. |
|
101
|
|
|
*/ |
|
102
|
8 |
|
public function setCachePath(string $value): self |
|
103
|
|
|
{ |
|
104
|
8 |
|
$this->cachePath = $value; |
|
105
|
8 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
/** |
|
108
|
|
|
* Turn on or off preserving of Eve API data by this preserver. |
|
109
|
|
|
* |
|
110
|
|
|
* Allows class to stay registered for events but be enabled or disabled during runtime. |
|
111
|
|
|
* |
|
112
|
|
|
* @param bool $value |
|
113
|
|
|
* |
|
114
|
|
|
* @return self Fluent interface. |
|
115
|
|
|
*/ |
|
116
|
8 |
|
public function setPreserve(bool $value = true): self |
|
117
|
|
|
{ |
|
118
|
8 |
|
$this->preserve = $value; |
|
119
|
8 |
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
/** |
|
122
|
|
|
* @return string |
|
123
|
|
|
* @throws \LogicException |
|
124
|
|
|
*/ |
|
125
|
5 |
|
protected function getCachePath() |
|
126
|
|
|
{ |
|
127
|
5 |
|
if (null === $this->cachePath || '' === $this->cachePath) { |
|
128
|
1 |
|
$mess = 'Trying to use cachePath before it was set'; |
|
129
|
1 |
|
throw new \LogicException($mess); |
|
130
|
|
|
} |
|
131
|
4 |
|
return $this->cachePath; |
|
132
|
|
|
} |
|
133
|
|
|
/** |
|
134
|
|
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
6 |
|
private function shouldPreserve() |
|
137
|
|
|
{ |
|
138
|
6 |
|
return $this->preserve; |
|
139
|
|
|
} |
|
140
|
|
|
/** |
|
141
|
|
|
* @var string $cachePath |
|
142
|
|
|
*/ |
|
143
|
|
|
private $cachePath; |
|
144
|
|
|
/** |
|
145
|
|
|
* @var bool $preserve |
|
146
|
|
|
*/ |
|
147
|
|
|
private $preserve = false; |
|
148
|
|
|
} |
|
149
|
|
|
|