• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

68publishers / image-storage / 10120426966

27 Jul 2024 03:14AM UTC coverage: 92.95% (+0.2%) from 92.757%
10120426966

push

github

tg666
Fixed PHPStan

1503 of 1617 relevant lines covered (92.95%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.66
/src/Resource/ResourceFactory.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace SixtyEightPublishers\ImageStorage\Resource;
6

7
use Intervention\Image\Exception\ImageException;
8
use Intervention\Image\Image;
9
use Intervention\Image\ImageManager;
10
use League\Flysystem\FilesystemException as LeagueFilesystemException;
11
use League\Flysystem\FilesystemReader;
12
use League\Flysystem\UnableToReadFile;
13
use SixtyEightPublishers\FileStorage\Exception\FileNotFoundException;
14
use SixtyEightPublishers\FileStorage\Exception\FilesystemException;
15
use SixtyEightPublishers\FileStorage\PathInfoInterface;
16
use SixtyEightPublishers\FileStorage\Resource\ResourceFactoryInterface;
17
use SixtyEightPublishers\FileStorage\Resource\ResourceInterface;
18
use SixtyEightPublishers\ImageStorage\Modifier\Facade\ModifierFacadeInterface;
19
use SixtyEightPublishers\ImageStorage\PathInfoInterface as ImagePathInfoInterface;
20
use SixtyEightPublishers\ImageStorage\Persistence\ImagePersisterInterface;
21
use function error_clear_last;
22
use function error_get_last;
23
use function file_put_contents;
24
use function filter_var;
25
use function fopen;
26
use function sprintf;
27
use function stream_context_create;
28
use function sys_get_temp_dir;
29
use function tempnam;
30

31
final class ResourceFactory implements ResourceFactoryInterface
32
{
33
    public function __construct(
1✔
34
        private readonly FilesystemReader $filesystemReader,
35
        private readonly ImageManager $imageManager,
36
        private readonly ModifierFacadeInterface $modifierFacade,
37
    ) {}
1✔
38

39
    /**
40
     * @throws FileNotFoundException
41
     * @throws LeagueFilesystemException
42
     * @throws FilesystemException
43
     */
44
    public function createResource(PathInfoInterface $pathInfo): ResourceInterface
1✔
45
    {
46
        if ($pathInfo instanceof ImagePathInfoInterface && null !== $pathInfo->getModifiers()) {
1✔
47
            $sourcePathInfo = $pathInfo->withModifiers(null);
1✔
48
        }
49

50
        $path = isset($sourcePathInfo) ? $sourcePathInfo->getPath() : $pathInfo->getPath();
1✔
51
        $filesystemPath = ImagePersisterInterface::FILESYSTEM_PREFIX_SOURCE . $path;
1✔
52

53
        if (false === $this->filesystemReader->fileExists($filesystemPath)) {
1✔
54
            throw new FileNotFoundException($path);
1✔
55
        }
56

57
        try {
58
            $source = $this->filesystemReader->read($filesystemPath);
1✔
59
        } catch (UnableToReadFile $e) {
1✔
60
            throw new FilesystemException(sprintf(
1✔
61
                'Unable to read file "%s".',
1✔
62
                $path,
1✔
63
            ), 0, $e);
1✔
64
        } catch (LeagueFilesystemException $e) {
1✔
65
            throw new FilesystemException($e->getMessage(), 0, $e);
1✔
66
        }
67

68
        return $this->createTmpFileResource(
1✔
69
            pathInfo: $pathInfo,
1✔
70
            location: $path,
71
            source: $source,
72
        );
73
    }
74

75
    public function createResourceFromFile(PathInfoInterface $pathInfo, string $filename): ResourceInterface
1✔
76
    {
77
        if (!filter_var($filename, FILTER_VALIDATE_URL)) {
1✔
78
            return new ImageResource(
1✔
79
                pathInfo: $pathInfo,
1✔
80
                image: $this->makeImage(
1✔
81
                    source: $filename,
1✔
82
                    location: $filename,
83
                ),
84
                localFilename: $filename,
85
                modifierFacade: $this->modifierFacade,
1✔
86
            );
87
        }
88

89
        error_clear_last();
×
90

91
        $context = stream_context_create(
×
92
            options: [
93
                'http' => [
94
                    'method' => 'GET',
×
95
                    'protocol_version' => 1.1,
96
                    'header' => "Accept-language: en\r\n" . "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36\r\n",
97
                ],
98
            ],
99
        );
100

101
        $source = @fopen(
×
102
            filename: $filename,
×
103
            mode: 'rb',
×
104
            context: $context,
×
105
        );
106

107
        if (false === $source) {
×
108
            throw new FilesystemException(
×
109
                message: sprintf(
×
110
                    'Can not read stream from url "%s". %s',
×
111
                    $filename,
×
112
                    error_get_last()['message'] ?? '',
×
113
                ),
114
            );
115
        }
116

117
        return $this->createTmpFileResource(
×
118
            pathInfo: $pathInfo,
×
119
            location: $filename,
120
            source: $source,
121
        );
122
    }
123

124
    /**
125
     * @throws FilesystemException
126
     */
127
    private function createTmpFileResource(PathInfoInterface $pathInfo, string $location, mixed $source): TmpFileImageResource
1✔
128
    {
129
        $tmpFilename = (string) tempnam(sys_get_temp_dir(), '68Publishers_ImageStorage');
1✔
130

131
        if (false === file_put_contents($tmpFilename, $source)) {
1✔
132
            throw new FilesystemException(sprintf(
×
133
                'Unable to write tmp file for "%s".',
×
134
                $location,
×
135
            ));
136
        }
137

138
        return new TmpFileImageResource(
1✔
139
            pathInfo: $pathInfo,
1✔
140
            image: $this->makeImage(
1✔
141
                source: $tmpFilename,
1✔
142
                location: $location,
143
            ),
144
            modifierFacade: $this->modifierFacade,
1✔
145
            tmpFile: new TmpFile($tmpFilename),
1✔
146
        );
147
    }
148

149
    /**
150
     * @throws FilesystemException
151
     */
152
    private function makeImage(mixed $source, string $location): Image
1✔
153
    {
154
        try {
155
            return $this->imageManager->make($source);
1✔
156
        } catch (ImageException $e) {
×
157
            throw new FilesystemException(
×
158
                message: sprintf(
×
159
                    'Unable to create image "%s". %s',
×
160
                    $location,
×
161
                    $e->getMessage(),
×
162
                ),
163
                previous: $e,
164
            );
165
        }
166
    }
167
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc