|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ozean12\WebTranslateItBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Ozean12\WebTranslateItBundle\DTO\ProjectFileDTO; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class WebTranslateItFileService. |
|
9
|
|
|
*/ |
|
10
|
|
|
class WebTranslateItFileService implements FileServiceInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var TranslationRepositoryInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
private $translationRepository; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* WebTranslateItFileService constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param TranslationRepositoryInterface $translationRepository |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(TranslationRepositoryInterface $translationRepository) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->translationRepository = $translationRepository; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Check if file content should be pulled. |
|
29
|
|
|
* |
|
30
|
|
|
* If local file does not exist |
|
31
|
|
|
* OR local file last update datetime is before the remote file update datetime |
|
32
|
|
|
* OR local file hash is different from the remote file hash |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $realFilePath |
|
35
|
|
|
* @param ProjectFileDTO $projectFile |
|
36
|
|
|
* |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
|
|
public function shouldBeUpdated($realFilePath, ProjectFileDTO $projectFile) |
|
40
|
|
|
{ |
|
41
|
|
|
return !file_exists($realFilePath) |
|
42
|
|
|
|| filemtime($realFilePath) < $projectFile->getUpdatedAt()->getTimestamp() |
|
43
|
|
|
|| $projectFile->getHashFile() !== sha1_file($realFilePath) |
|
44
|
|
|
; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function update($filePath, ProjectFileDTO $projectFile) |
|
51
|
|
|
{ |
|
52
|
|
|
$newFileContent = $this->translationRepository->pullFile($projectFile->getName()); |
|
53
|
|
|
file_put_contents($filePath, $newFileContent); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function prepare($directory) |
|
60
|
|
|
{ |
|
61
|
|
|
if (!file_exists($directory)) { |
|
62
|
|
|
mkdir($directory); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|