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