Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
27 / 27
DownloadController
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
27 / 27
 __construct
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 downloadAction
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
10 / 10
 getMimeType
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 getFilePath
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
9 / 9
<?php
namespace Bone\Controller;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Stream;
class DownloadController
{
    /** @var string $uploadsDirectory */
    private $uploadsDirectory;
    public function __construct(string $uploadsDirectory)
    {
        if (!is_dir($uploadsDirectory)) {
            throw new InvalidArgumentException('Directory ' . $uploadsDirectory . ' not found');
        }
        $this->uploadsDirectory = $uploadsDirectory;
    }
    /**
     * @param ServerRequestInterface $request
     * @param array $args
     * @return ResponseInterface
     * @throws ControllerException
     */
    public function downloadAction(ServerRequestInterface $request, array $args): ResponseInterface
    {
        $queryParams = $request->getQueryParams();
        $path = $this->getFilePath($queryParams);
        $mimeType = $this->getMimeType($path);
        $contents = file_get_contents($path);
        $stream = new Stream('php://memory', 'r+');
        $stream->write($contents);
        $response = new Response();
        $response = $response->withBody($stream);
        $response = $response->withHeader('Content-Type', $mimeType);
        return $response;
    }
    /**
     * @param string $path
     * @return string
     */
    private function getMimeType(string $path): string
    {
        $finfo = finfo_open(FILEINFO_MIME); // return mime type
        $mimeType = finfo_file($finfo, $path);
        finfo_close($finfo);
        return $mimeType;
    }
    /**
     * @param array $queryParams
     * @return string
     * @throws ControllerException
     */
    private function getFilePath(array $queryParams): string
    {
        if (!isset($queryParams['file'])) {
            throw new ControllerException('Invalid Request.', 400);
        }
        $file = $queryParams['file'];
        $path = $this->uploadsDirectory . $file;
        if (file_exists('public' . $file)) {
            $path = 'public' . $file;
        } else if (!file_exists($path)) {
            throw new ControllerException($path . ' not found.', 404);
        }
        return $path;
    }
}