|
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 DomainException; |
|
37
|
|
|
use FilePathNormalizer\FilePathNormalizerTrait; |
|
38
|
|
|
use InvalidArgumentException; |
|
39
|
|
|
use LogicException; |
|
40
|
|
|
use Yapeal\Event\EveApiEventEmitterTrait; |
|
41
|
|
|
use Yapeal\Event\EveApiEventInterface; |
|
42
|
|
|
use Yapeal\Event\MediatorInterface; |
|
43
|
|
|
use Yapeal\Log\Logger; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Class CachePreserver |
|
47
|
|
|
*/ |
|
48
|
|
|
class CachePreserver |
|
49
|
|
|
{ |
|
50
|
|
|
use CommonFileHandlingTrait, EveApiEventEmitterTrait, FilePathNormalizerTrait; |
|
51
|
|
|
/** |
|
52
|
|
|
* @param string|null $cachePath |
|
53
|
|
|
* |
|
54
|
|
|
* @throws InvalidArgumentException |
|
55
|
|
|
* @throws DomainException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct($cachePath = null) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->setCachePath($cachePath); |
|
60
|
|
|
} |
|
61
|
|
|
/** |
|
62
|
|
|
* @param EveApiEventInterface $event |
|
63
|
|
|
* @param string $eventName |
|
64
|
|
|
* @param MediatorInterface $yem |
|
65
|
|
|
* |
|
66
|
|
|
* @return EveApiEventInterface |
|
67
|
|
|
* @throws LogicException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function preserveEveApi(EveApiEventInterface $event, $eventName, MediatorInterface $yem) |
|
70
|
|
|
{ |
|
71
|
|
|
$data = $event->getData(); |
|
72
|
|
|
$this->setYem($yem); |
|
73
|
|
|
$yem->triggerLogEvent( |
|
74
|
|
|
'Yapeal.Log.log', |
|
75
|
|
|
Logger::DEBUG, |
|
76
|
|
|
$this->getReceivedEventMessage($data, $eventName, __CLASS__) |
|
77
|
|
|
); |
|
78
|
|
|
// BaseSection/ApiHash.xml |
|
79
|
|
|
$cacheFile = sprintf( |
|
80
|
|
|
'%1$s%2$s/%3$s%4$s.xml', |
|
81
|
|
|
$this->getCachePath(), |
|
82
|
|
|
ucfirst($data->getEveApiSectionName()), |
|
83
|
|
|
ucfirst($data->getEveApiName()), |
|
84
|
|
|
$data->getHash() |
|
85
|
|
|
); |
|
86
|
|
|
$xml = $data->getEveApiXml(); |
|
87
|
|
|
if (false === $xml) { |
|
88
|
|
|
$event->setHandledSufficiently(); |
|
89
|
|
|
} |
|
90
|
|
|
// Insures retriever never see partly written file by deleting old file and using temp file for writing. |
|
91
|
|
|
if (false === $this->safeFileWrite($xml, $cacheFile, $yem)) { |
|
|
|
|
|
|
92
|
|
|
return $event; |
|
93
|
|
|
} |
|
94
|
|
|
return $event->setHandledSufficiently(); |
|
95
|
|
|
} |
|
96
|
|
|
/** |
|
97
|
|
|
* @param string|null $value |
|
98
|
|
|
* |
|
99
|
|
|
* @return self Fluent interface. |
|
100
|
|
|
* @throws DomainException |
|
101
|
|
|
* @throws InvalidArgumentException |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setCachePath($value = null) |
|
104
|
|
|
{ |
|
105
|
|
|
if ($value === null) { |
|
106
|
|
|
$value = dirname(dirname(__DIR__)) . '/cache/'; |
|
107
|
|
|
} |
|
108
|
|
|
if (!is_string($value)) { |
|
109
|
|
|
$mess = 'Cache path MUST be string, but was given ' . gettype($value); |
|
110
|
|
|
throw new InvalidArgumentException($mess); |
|
111
|
|
|
} |
|
112
|
|
|
if ('' === $this->cachePath) { |
|
113
|
|
|
$mess = 'Cache path can NOT be empty'; |
|
114
|
|
|
throw new DomainException($mess); |
|
115
|
|
|
} |
|
116
|
|
|
$this->cachePath = $this->getFpn() |
|
117
|
|
|
->normalizePath($value); |
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
/** |
|
121
|
|
|
* @return string |
|
122
|
|
|
* @throws LogicException |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function getCachePath() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->cachePath; |
|
127
|
|
|
} |
|
128
|
|
|
/** |
|
129
|
|
|
* @type string $cachePath |
|
130
|
|
|
*/ |
|
131
|
|
|
protected $cachePath; |
|
132
|
|
|
} |
|
133
|
|
|
|
This check looks for type mismatches where the missing type is
false. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTimeobject or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalsebefore passing on the value to another function or method that may not be able to handle afalse.