21
21
use Magento \MediaGalleryMetadataApi \Model \SegmentInterfaceFactory ;
22
22
23
23
/**
24
- * Jpeg file reader.
24
+ * Jpeg file reader
25
25
*/
26
26
class ReadFile implements ReadFileInterface
27
27
{
@@ -79,7 +79,27 @@ public function __construct(
79
79
}
80
80
81
81
/**
82
- * {@inheritdoc}
82
+ * Is reader applicable
83
+ *
84
+ * @param string $path
85
+ * @return bool
86
+ * @throws FileSystemException
87
+ */
88
+ private function isApplicable (string $ path ): bool
89
+ {
90
+ $ resource = $ this ->driver ->fileOpen ($ path , 'rb ' );
91
+ try {
92
+ $ marker = $ this ->readMarker ($ resource );
93
+ } catch (LocalizedException $ exception ) {
94
+ return false ;
95
+ }
96
+ $ this ->driver ->fileClose ($ resource );
97
+
98
+ return $ marker == self ::MARKER_IMAGE_FILE_START ;
99
+ }
100
+
101
+ /**
102
+ * @inheritdoc
83
103
*/
84
104
public function execute (string $ path ): FileInterface
85
105
{
@@ -90,7 +110,7 @@ public function execute(string $path): FileInterface
90
110
$ resource = $ this ->driver ->fileOpen ($ path , 'rb ' );
91
111
$ marker = $ this ->readMarker ($ resource );
92
112
93
- if (self :: MARKER_IMAGE_FILE_START != $ marker ) {
113
+ if ($ marker != self :: MARKER_IMAGE_FILE_START ) {
94
114
$ this ->driver ->fileClose ($ resource );
95
115
96
116
throw new ValidatorException (__ ('Not a JPEG image ' ));
@@ -99,56 +119,37 @@ public function execute(string $path): FileInterface
99
119
do {
100
120
$ marker = $ this ->readMarker ($ resource );
101
121
$ segments [] = $ this ->readSegment ($ resource , ord ($ marker ));
102
- } while ((self :: MARKER_IMAGE_START != $ marker ) && (!$ this ->driver ->endOfFile ($ resource )));
122
+ } while (($ marker != self :: MARKER_IMAGE_START ) && (!$ this ->driver ->endOfFile ($ resource )));
103
123
104
- if (self :: MARKER_IMAGE_START != $ marker ) {
124
+ if ($ marker != self :: MARKER_IMAGE_START ) {
105
125
throw new LocalizedException (__ ('File is corrupted ' ));
106
126
}
107
127
108
128
$ segments [] = $ this ->segmentFactory ->create ([
109
129
'name ' => 'CompressedImage ' ,
110
- 'data ' => $ this ->readCompressedImage ($ resource ),
130
+ 'data ' => $ this ->readCompressedImage ($ resource )
111
131
]);
112
132
113
133
$ this ->driver ->fileClose ($ resource );
114
134
115
135
return $ this ->fileFactory ->create ([
116
136
'path ' => $ path ,
117
- 'segments ' => $ segments,
137
+ 'segments ' => $ segments
118
138
]);
119
139
}
120
140
121
141
/**
122
- * Is reader applicable.
123
- *
124
- * @throws FileSystemException
125
- */
126
- private function isApplicable (string $ path ): bool
127
- {
128
- $ resource = $ this ->driver ->fileOpen ($ path , 'rb ' );
129
-
130
- try {
131
- $ marker = $ this ->readMarker ($ resource );
132
- } catch (LocalizedException $ exception ) {
133
- return false ;
134
- }
135
- $ this ->driver ->fileClose ($ resource );
136
-
137
- return self ::MARKER_IMAGE_FILE_START == $ marker ;
138
- }
139
-
140
- /**
141
- * Read jpeg marker.
142
+ * Read jpeg marker
142
143
*
143
144
* @param resource $resource
144
- *
145
+ * @return string
145
146
* @throws FileSystemException
146
147
*/
147
148
private function readMarker ($ resource ): string
148
149
{
149
150
$ data = $ this ->read ($ resource , self ::TWO_BYTES );
150
151
151
- if (self :: MARKER_PREFIX != $ data [0 ]) {
152
+ if ($ data [0 ] != self :: MARKER_PREFIX ) {
152
153
$ this ->driver ->fileClose ($ resource );
153
154
154
155
throw new LocalizedException (__ ('File is corrupted ' ));
@@ -158,10 +159,10 @@ private function readMarker($resource): string
158
159
}
159
160
160
161
/**
161
- * Read compressed image.
162
+ * Read compressed image
162
163
*
163
164
* @param resource $resource
164
- *
165
+ * @return string
165
166
* @throws FileSystemException
166
167
*/
167
168
private function readCompressedImage ($ resource ): string
@@ -173,18 +174,19 @@ private function readCompressedImage($resource): string
173
174
174
175
$ endOfImageMarkerPosition = strpos ($ compressedImage , self ::MARKER_PREFIX .self ::MARKER_IMAGE_END );
175
176
176
- if (false !== $ endOfImageMarkerPosition ) {
177
+ if ($ endOfImageMarkerPosition !== false ) {
177
178
$ compressedImage = substr ($ compressedImage , 0 , $ endOfImageMarkerPosition );
178
179
}
179
180
180
181
return $ compressedImage ;
181
182
}
182
183
183
184
/**
184
- * Read jpeg segment.
185
+ * Read jpeg segment
185
186
*
186
187
* @param resource $resource
187
- *
188
+ * @param int $segmentType
189
+ * @return SegmentInterface
188
190
* @throws FileSystemException
189
191
*/
190
192
private function readSegment ($ resource , int $ segmentType ): SegmentInterface
@@ -194,15 +196,16 @@ private function readSegment($resource, int $segmentType): SegmentInterface
194
196
195
197
return $ this ->segmentFactory ->create ([
196
198
'name ' => $ this ->segmentNames ->getSegmentName ($ segmentType ),
197
- 'data ' => $ this ->read ($ resource , $ segmentSize ),
199
+ 'data ' => $ this ->read ($ resource , $ segmentSize )
198
200
]);
199
201
}
200
202
201
203
/**
202
- * Read wrapper.
204
+ * Read wrapper
203
205
*
204
206
* @param resource $resource
205
- *
207
+ * @param int $length
208
+ * @return string
206
209
* @throws FileSystemException
207
210
*/
208
211
private function read ($ resource , int $ length ): string
0 commit comments