Passed
Push — master ( 4ab488...bcfbc7 )
by Bálint
03:58
created

NorthWindStreamProvider   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 308
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 54
dl 0
loc 308
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getReadStreamUri2() 0 4 1
A getStreamContentType2() 0 8 2
A getStreamContentType() 0 10 2
A getReadStreamUri() 0 4 1
A getStreamETag() 0 19 3
A getReadStream2() 0 29 3
A getReadStream() 0 33 3
A getStreamETag2() 0 4 1
1
<?php
2
3
require_once 'POData\Providers\Stream\IDataServiceStreamProvider2.php';
4
/**
5
 * Stream provider for northwind service.
6
 */
7
require_once 'POData\Common\ODataException.php';
8
require_once 'NorthWindMetadata.php';
9
use POData\Providers\Metadata\ResourceStreamInfo;
10
use POData\Providers\Stream\IStreamProvider2;
11
use POData\Common\ODataException;
12
13
14
class NorthWindStreamProvider implements IStreamProvider2
15
{
16
    // NOTE: update this path as per your configuration
17
    const IMAGE_PATH_ROOT = 'D:\\Projects\\ODataPHPProducer\\services\\NorthWind\\images\\';
18
19
    //Begin IStreamProvider methods implementation
20
     
21
    /**
22
     * Method invoked by the data services framework to retrieve the default 
23
     * stream associated with the entity instance specified by the entity parameter.
24
     *
25
     * @param object              $entity               The stream returned should be
26
     *                                                  the default stream associated
27
     *                                                  with this entity instance.
28
     * @param string              $eTag                 The etag value sent by the 
29
     *                                                  client (as the value of an 
30
     *                                                  If[-None-]Match header) 
31
     *                                                  as part of the HTTP request, 
32
     *                                                  This parameter will be
33
     *                                                  null if no If[-None-]Match 
34
     *                                                  header was present.
35
     * @param boolean             $checkETagForEquality True if an value of the etag
36
     *                                                  parameter was sent 
37
     *                                                  to the server as the value
38
     *                                                  of an If-Match HTTP
39
     *                                                  request header, 
40
     *                                                  False if an value of the etag
41
     *                                                  parameter was sent to the 
42
     *                                                  server as the the value
43
     *                                                  of an If-None-Match HTTP 
44
     *                                                  request header null if
45
     *                                                  the HTTP request for the 
46
     *                                                  stream was not a
47
     *                                                  conditional request.
48
     * @param WebOperationContext $operationContext     A reference to the context
0 ignored issues
show
Bug introduced by
The type WebOperationContext was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
49
     *                                                  for the current operation.
50
     *
51
     * @return string A valid  default stream which is associated with the entity, 
52
     * Null should never be returned from this method.
53
     *
54
     * @throws ODataException if a valid stream cannot be returned.  
55
     * Null should never be returned from this method.
56
     */
57
    public function getReadStream($entity, $eTag, 
58
        $checkETagForEquality, 
59
        $operationContext
60
    ) {
61
        /**
62
        if (!is_null($checkETagForEquality)) {
63
            throw new ODataException(
64
                'This service does not support the ETag header for a media resource', 
65
                400
66
            );
67
        }**/
68
        // NOTE: In this impementation we are not checking the eTag equality
69
        // We will return the stream irrespective of the whether the eTag match of not
70
71
        if (!($entity instanceof Employee)) {
72
            throw new ODataException(
73
                'Internal Server Error.', 
74
                500
75
            );
76
        }
77
78
        $filePath = self::IMAGE_PATH_ROOT 
79
            . 'Employee_' . $entity->EmployeeID 
80
            . '.jpg';
81
        if (file_exists($filePath)) {
82
            $handle = fopen($filePath, 'r');
83
            $stream = fread($handle, filesize($filePath));
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
            $stream = fread(/** @scrutinizer ignore-type */ $handle, filesize($filePath));
Loading history...
84
            fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
            fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
85
            return $stream;
86
        } else {
87
            throw new ODataException(
88
                'The image file could not be found', 
89
                500
90
            );
91
        }
92
    }
93
94
    /**
95
     * Method invoked by the data services framework to obtain the 
96
     * IANA content type (aka media type) of the stream associated 
97
     * with the specified entity.  This metadata is needed when 
98
     * constructing the payload for the Media Link Entry associated 
99
     * with the stream (aka Media Resource) or setting the Content-Type
100
     * HTTP response header.
101
     * 
102
     * @param object              $entity           The entity instance associated 
103
     *                                              with the stream for which 
104
     *                                              the content type is to
105
     *                                              be obtained.
106
     * @param WebOperationContext $operationContext A reference to the context 
107
     *                                              for the current operation
108
     * 
109
     * @return string Valid Content-Type string for the stream 
110
     * associated with the entity.
111
     *
112
     * @throws ODataException if a valid stream content type 
113
     * associated with the entity specified could not be returned.
114
     */
115
    public function getStreamContentType($entity, $operationContext)
116
    {
117
        if (!($entity instanceof Employee)) {
118
            throw new ODataException(
119
                'Internal Server Error.', 
120
                500
121
            );
122
        }
123
124
        return 'image/jpeg';
125
    }
126
127
    /**
128
     * Method invoked by the data services framework to obtain the ETag 
129
     * of the stream associated with the entity specified. 
130
     * This metadata is needed when constructing the
131
     * payload for the Media Link Entry associated with the stream 
132
     * (aka Media Resource) as well as to be used as the 
133
     * value of the ETag HTTP response header.
134
     *
135
     * @param object              $entity           The entity instance 
136
     *                                              associated with the
137
     *                                              stream for which an 
138
     *                                              etag is to be obtained.
139
     * @param WebOperationContext $operationContext A reference to the context
140
     *                                              for the current
141
     *                                              operation.
142
     *
143
     * @return string ETag of the stream associated with the entity specified.
144
     */
145
    public function getStreamETag($entity, $operationContext)
146
    {
147
        if (!($entity instanceof Employee)) {
148
            throw new ODataException(
149
                'Internal Server Error.', 
150
                500
151
            );
152
        }
153
154
        $lastModifiedTime = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $lastModifiedTime is dead and can be removed.
Loading history...
155
        $filePath = self::IMAGE_PATH_ROOT . 'Employee_' . $entity->EmployeeID . '.jpg';
156
        if (file_exists($filePath)) {
157
            $lastModifiedTime = date("\"m-d-Y H:i:s\"", filemtime($filePath));
158
        } else {
159
            // The no stream associated the the requested enttiy, so no eTag
160
            return null;
161
        }
162
163
        return $lastModifiedTime;
164
    }
165
166
    /**
167
     * This method is invoked by the data services framework 
168
     * to obtain the URI clients should
169
     * use when making retrieve (ie. GET) requests to the stream(ie. Media Resource).
170
     * This metadata is needed when constructing the payload for the Media Link Entry
171
     * associated with the stream (aka Media Resource).
172
     *
173
     * @param object              $entity           The entity instance 
174
     *                                              associated with the
175
     *                                              stream for which a read 
176
     *                                              stream URI is to
177
     *                                              be obtained.
178
     * @param WebOperationContext $operationContext A reference to the 
179
     *                                              context for the current
180
     *                                              operation
181
     *
182
     * @return string The URI clients should use when making retrieve 
183
     * (ie. GET) requests to the stream(ie. Media Resource).
184
     */
185
    public function getReadStreamUri($entity, $operationContext)
186
    {
187
        //let library creates default media url.
188
        return null;
189
    }
190
191
    //End IStreamProvider methods implementation
192
193
    //Begin IStreamProvider2 methods implementation
194
    /**
195
     * This method is invoked by the data services framework to retrieve the named stream
196
     * associated with the entity instance specified by the entity parameter.
197
     *
198
     * @param object              $entity               The stream returned should be the default
199
     *                                                  stream associated with this entity instance.
200
     * @param ResourceStreamInfo  $resourceStreamInfo   The ResourceStreamInfo instance that describes
201
     *                                                  the named stream.
202
     * @param string              $eTag                 The etag value sent by the client (as the
203
     *                                                  value of an If[-None-]Match header) as part
204
     *                                                  of the HTTP request, This parameter will be
205
     *                                                  null if no If[-None-]Match header was present.
206
     * @param boolean             $checkETagForEquality True if an value of the etag parameter was sent
207
     *                                                  to the server as the value of an If-Match HTTP
208
     *                                                  request header, False if an value of the etag
209
     *                                                  parameter was sent to the server as the the value
210
     *                                                  of an If-None-Match HTTP request header null if
211
     *                                                  the HTTP request for the stream was not a
212
     *                                                  conditional request.
213
     * @param WebOperationContext $operationContext     A reference to the context for the current operation.
214
     *
215
     * @return string A valid stream the data service use to query/read a named stream which is
216
     * associated with the $entity. Null may be returned from this method if the requested named
217
     * stream has not been created since the creation of $entity. The data service will respond 
218
     * with 204 if this method returns null.
219
     *
220
     * @throws ODataException if a valid stream or null cannot be returned for the given arguments.
221
     */
222
    public function getReadStream2($entity, ResourceStreamInfo $resourceStreamInfo, 
223
        $eTag, $checkETagForEquality, 
224
        $operationContext
225
    ) {
226
        /**
227
        if (!is_null($checkETagForEquality)) {
228
            throw new ODataException(
229
                'This service does not support the ETag header for a media resource', 
230
                400
231
            );
232
        }
233
         **/
234
235
        if (!($entity instanceof Employee)) {
236
            throw new ODataException('Internal Server Error.', 500);
237
        }
238
        
239
        $filePath = self::IMAGE_PATH_ROOT . 'Employee_' 
240
            . $entity->EmployeeID 
241
            . '_' 
242
            . $resourceStreamInfo->getName() 
243
            . '.png';
244
        if (file_exists($filePath)) {
245
            $handle = fopen($filePath, 'r');
246
            $stream = fread($handle, filesize($filePath));
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

246
            $stream = fread(/** @scrutinizer ignore-type */ $handle, filesize($filePath));
Loading history...
247
            fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

247
            fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
248
            return $stream;
249
        } else {
250
            throw new ODataException('The image file could not be found', 500);
251
        }
252
    }
253
254
    /**
255
     * This method is invoked by the data services framework to obtain the IANA content type
256
     * (aka media type) of the named stream associated with the specified entity.  This
257
     * metadata is needed when constructing the payload for the entity associated with the
258
     * named stream or setting the Content-Type HTTP response header.
259
     *
260
     * @param object              $entity             The entity instance associated with the
261
     *                                                stream for which the content type is to
262
     *                                                be obtained.
263
     * @param ResourceStreamInfo  $resourceStreamInfo The ResourceStreamInfo instance that describes
264
     *                                                the named stream.
265
     * @param WebOperationContext $operationContext   A reference to the context for the current
266
     *                                                operation
267
     *
268
     * @return string Valid Content-Type string for the named stream associated with the entity.
269
     */
270
    public function getStreamContentType2($entity, ResourceStreamInfo $resourceStreamInfo, 
271
        $operationContext
272
    ) {
273
        if (!($entity instanceof Employee)) {
274
            throw new ODataException('Internal Server Error.', 500);
275
        }
276
277
        return 'image/png';
278
    }
279
280
    /**
281
     * This method is invoked by the data services framework to obtain the ETag of the
282
     * name stream associated with the entity specified. This metadata is needed when
283
     * constructing the payload for the entity associated with the named stream as well as
284
     * to be used as the value of the ETag HTTP response header.
285
     *
286
     * @param object              $entity             The entity instance associated with the
287
     *                                                stream for which an etag is to be obtained.
288
     * @param ResourceStreamInfo  $resourceStreamInfo The ResourceStreamInfo instance that describes
289
     *                                                the named stream.
290
     * @param WebOperationContext $operationContext   A reference to the context for the current
291
     *                                                operation.
292
     *
293
     * @return string ETag of the named stream associated with the entity specified.
294
     */
295
    public function getStreamETag2($entity, ResourceStreamInfo $resourceStreamInfo, 
296
        $operationContext
297
    ) {
298
        return null;
299
    }
300
301
    /**
302
     * This method is invoked by the data services framework to obtain the URI clients should
303
     * use when making retrieve (ie. GET) requests to the named stream.
304
     * This metadata is needed when constructing the payload for the entity associated with
305
     * the named stream.
306
     *
307
     * @param object              $entity             The entity instance associated with the
308
     *                                                stream for which a read stream URI is to
309
     *                                                be obtained.
310
     * @param ResourceStreamInfo  $resourceStreamInfo The ResourceStreamInfo instance that describes
311
     *                                                the named stream.
312
     * @param WebOperationContext $operationContext   A reference to the context for the current
313
     *                                                operation
314
     *
315
     * @return string The URI clients should use when making retrieve (ie. GET) requests to
316
     *                the stream(ie. Media Resource).
317
     */
318
    public function getReadStreamUri2($entity, ResourceStreamInfo $resourceStreamInfo, 
319
        $operationContext
320
    ) {
321
        return null;
322
    }
323
    
324
    //End IStreamProvider2 methods implementation
325
}
326