Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| MimeTypesSrcJsDelivr | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMimeTypes | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @author: Doug Wilbourne (dougwilbourne@gmail.com) |
| 5 | */ |
| 6 | |
| 7 | declare(strict_types=1); |
| 8 | |
| 9 | namespace pvc\http\mime; |
| 10 | |
| 11 | use pvc\http\err\MimeTypeCdnException; |
| 12 | use pvc\http\err\MimeTypesJsonDecodingException; |
| 13 | use pvc\interfaces\http\mime\MimeTypeFactoryInterface; |
| 14 | use pvc\interfaces\http\mime\MimeTypeInterface; |
| 15 | use pvc\interfaces\http\mime\MimeTypesSrcInterface; |
| 16 | |
| 17 | /** |
| 18 | * Class MimeTypesSrcJsDelivr |
| 19 | * |
| 20 | * when we get the json from the cdn and decode it, we will get an array of StdClass objects of the following shape. |
| 21 | * @phpstan-type MimeTypeShapeJsDelivr object{'source': string, 'extensions': ?array<string>, 'compressible': bool, 'charset': string} |
| 22 | */ |
| 23 | class MimeTypesSrcJsDelivr implements MimeTypesSrcInterface |
| 24 | { |
| 25 | /** |
| 26 | * this cdn is a compilation from apache, iana, and nginx. |
| 27 | * @see https://www.jsdelivr.com/package/npm/mime-db |
| 28 | */ |
| 29 | protected const CDN = 'https://cdn.jsdelivr.net/gh/jshttp/mime-db@master/db.json'; |
| 30 | |
| 31 | public function __construct( |
| 32 | protected MimeTypeFactoryInterface $mimeTypeFactory |
| 33 | = new MimeTypeFactory() |
| 34 | ) { |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * getMimeTypes |
| 39 | * @return array<string, MimeTypeInterface> |
| 40 | */ |
| 41 | public function getMimeTypes(): array |
| 42 | { |
| 43 | $result = []; |
| 44 | |
| 45 | if (!$fileContents = file_get_contents(self::CDN)) { |
| 46 | // @codeCoverageIgnoreStart |
| 47 | throw new MimeTypeCdnException(self::CDN); |
| 48 | // @codeCoverageIgnoreEnd |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * if there was a problem decoding the json, json_decode returns null. |
| 53 | * otherwise, it is an array where the key is the mime type name and |
| 54 | * the value is a StdClass object, from which we will make MimeType |
| 55 | * objects |
| 56 | */ |
| 57 | |
| 58 | /** @var null|array<string, MimeTypesSrcJsDelivr> $array */ |
| 59 | $array = json_decode($fileContents); |
| 60 | |
| 61 | if (is_null($array)) { |
| 62 | // @codeCoverageIgnoreStart |
| 63 | throw new MimeTypesJsonDecodingException(); |
| 64 | // @codeCoverageIgnoreEnd |
| 65 | } |
| 66 | |
| 67 | foreach ($array as $mimeTypeName => $obj) { |
| 68 | $mt = $this->mimeTypeFactory->makeMimeType(); |
| 69 | $mt->setMimeTypeName($mimeTypeName); |
| 70 | /** |
| 71 | * not all mime types have file extensions defined and if there are none defined, then the stdClass object |
| 72 | * simply does not have that property. |
| 73 | * @var array<string> $extensions |
| 74 | */ |
| 75 | $extensions = $obj->extensions ?? []; |
| 76 | $mt->setFileExtensions($extensions); |
| 77 | $result[$mimeTypeName] = $mt; |
| 78 | } |
| 79 | return $result; |
| 80 | } |
| 81 | } |