Completed
Push — ezp26175-exception_on_non_defa... ( 5bddc1...682842 )
by
unknown
13:22
created

TolerantIOService::createMissingBinaryFileByUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A TolerantIOService::logMissingFile() 0 7 2
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\IO;
10
11
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
12
use eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException;
13
use eZ\Publish\Core\IO\Exception\InvalidBinaryFileIdException;
14
use eZ\Publish\Core\IO\Values\BinaryFile;
15
use eZ\Publish\Core\IO\Values\MissingBinaryFile;
16
use Psr\Log\LoggerInterface;
17
18
/**
19
 * An extended IOService that tolerates physically missing files.
20
 *
21
 * Meant to be used on a "broken" instance where the storage directory isn't in sync with the database.
22
 *
23
 * Note that it will still return false when exists() is used.
24
 */
25
class TolerantIOService extends IOService
26
{
27
    /** @var \Psr\Log\LoggerInterface */
28
    protected $logger;
29
30
    public function setLogger(LoggerInterface $logger = null)
31
    {
32
        $this->logger = $logger;
33
    }
34
35
    /**
36
     * Deletes $binaryFile.
37
     *
38
     * @param \eZ\Publish\Core\IO\Values\BinaryFile $binaryFile
39
     *
40
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue If the binary file is invalid
41
     * @throws \eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException If the binary file isn't found
42
     */
43
    public function deleteBinaryFile(BinaryFile $binaryFile)
44
    {
45
        $this->checkBinaryFileId($binaryFile->id);
46
        $spiUri = $this->getPrefixedUri($binaryFile->id);
47
48
        try {
49
            $this->metadataHandler->delete($spiUri);
50
        } catch (BinaryFileNotFoundException $e) {
51
            $this->logMissingFile($binaryFile->uri);
52
            $logged = true;
53
        }
54
55
        try {
56
            $this->binarydataHandler->delete($spiUri);
57
        } catch (BinaryFileNotFoundException $e) {
58
            if (!isset($logged)) {
59
                $this->logMissingFile($binaryFile->uri);
60
            }
61
        }
62
    }
63
64
    /**
65
     * Loads the binary file with $binaryFileId.
66
     *
67
     * @param string $binaryFileId
68
     *
69
     * @return \eZ\Publish\Core\IO\Values\BinaryFile|\eZ\Publish\Core\IO\Values\MissingBinaryFile
70
     *
71
     * @throws \eZ\Publish\Core\IO\Exception\InvalidBinaryFileIdException
72
     */
73
    public function loadBinaryFile($binaryFileId)
74
    {
75
        $this->checkBinaryFileId($binaryFileId);
76
77
        if ($this->isAbsolutePath($binaryFileId)) {
78
            throw new InvalidBinaryFileIdException($binaryFileId, 'Binary file ids can not begin with a /');
79
        }
80
81
        try {
82
            $spiBinaryFile = $this->metadataHandler->load($this->getPrefixedUri($binaryFileId));
83
        } catch (BinaryFileNotFoundException $e) {
84
            $this->logMissingFile($binaryFileId);
85
86
            return new MissingBinaryFile([
87
                'id' => $binaryFileId,
88
                'uri' => $this->binarydataHandler->getUri($this->getPrefixedUri($binaryFileId)),
89
            ]);
90
        }
91
92
        if (!isset($spiBinaryFile->uri)) {
93
            $spiBinaryFile->uri = $this->binarydataHandler->getUri($spiBinaryFile->id);
94
        }
95
96
        return $this->buildDomainBinaryFileObject($spiBinaryFile);
97
    }
98
99
    public function loadBinaryFileByUri($binaryFileUri)
100
    {
101
        try {
102
            $binaryFileId = $this->removeUriPrefix($this->binarydataHandler->getIdFromUri($binaryFileUri));
103
        } catch (InvalidArgumentException $e) {
104
            $this->logMissingFile($binaryFileUri);
105
106
            return new MissingBinaryFile(['uri' => $binaryFileUri]);
107
        }
108
109
        try {
110
            return $this->loadBinaryFile($binaryFileId);
111
        } catch (BinaryFileNotFoundException $e) {
112
            $this->logMissingFile($binaryFileUri);
113
114
            return new MissingBinaryFile([
115
                'id' => $binaryFileId,
116
                'uri' => $this->binarydataHandler->getUri($this->getPrefixedUri($binaryFileId)),
117
            ]);
118
        }
119
    }
120
121
    private function logMissingFile($id)
122
    {
123
        if (!isset($this->logger)) {
124
            return;
125
        }
126
        $this->logger->info("BinaryFile with id $id not found");
127
    }
128
}
129