Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Stream
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 openForReading
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 close
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace pvc\http;
4
5use pvc\http\err\InvalidResourceException;
6use pvc\http\err\InvalidStreamHandleException;
7use pvc\http\err\MimeTypesUnreadableStreamException;
8use pvc\interfaces\http\UrlInterface;
9
10class Stream
11{
12    /**
13     * @param UrlInterface $url
14     * @return resource
15     * @throws MimeTypesUnreadableStreamException
16     */
17    public static function openForReading(UrlInterface $url)
18    {
19        $urlString = $url->render();
20
21        if (!$handle = fopen($urlString, 'r')) {
22            throw new MimeTypesUnreadableStreamException($urlString);
23        }
24        return $handle;
25    }
26
27    /**
28     * @param resource $handle
29     * @return void
30     * @throws InvalidResourceException
31     * @throws InvalidStreamHandleException
32     */
33    public static function close($handle): void
34    {
35        if (!is_resource($handle)) {
36            throw new InvalidResourceException();
37        }
38        if (get_resource_type($handle) !== 'stream') {
39            throw new InvalidStreamHandleException();
40        }
41        fclose($handle);
42    }
43}