DownloadController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 5
eloc 16
dl 0
loc 45
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A downloadJson() 0 11 2
A downloadZip() 0 15 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: famoser
5
 * Date: 07/06/2016
6
 * Time: 17:54
7
 */
8
9
namespace Famoser\XKCDCache\Controllers;
10
11
use Famoser\XKCDCache\Controllers\Base\BaseController;
12
use Famoser\XKCDCache\Entities\Comic;
13
use Famoser\XKCDCache\Exceptions\ServerException;
14
use Famoser\XKCDCache\Types\ServerError;
15
use Slim\Http\Request;
16
use Slim\Http\Response;
17
18
/**
19
 * the download controller exposes the two downloadable files
20
 *
21
 * @package Famoser\XKCDCache\Controllers
22
 */
23
class DownloadController extends BaseController
24
{
25
    /**
26
     * show basic info about this application
27
     *
28
     * @param Request $request
29
     * @param Response $response
30
     * @return Response
31
     * @throws ServerException
32
     */
33 1
    public function downloadZip(/** @scrutinizer ignore-unused */ Request $request, Response $response)
34
    {
35 1
        $newestComic = $this->getCacheService()->getNewestComic();
36 1
        if (!($newestComic instanceof Comic)) {
37 1
            throw new ServerException(ServerError::CACHE_EMPTY);
38
        }
39
40 1
        $newestZip = $this->getCacheService()->getNewestZip();
41 1
        if ($newestZip === false) {
42
            throw new ServerException(ServerError::ZIP_NOT_FOUND);
43
        }
44
45 1
        $fileSize = $this->getCacheService()->getFileSizeOfZip($newestZip);
46 1
        $fileContent = $this->getCacheService()->getContentOfZip($newestZip);
47 1
        return $this->returnRawFile($response, "application/zip", "xkcd_comics_" . $newestZip . ".zip", $fileSize, $fileContent);
48
    }
49
50
    /**
51
     * show api info as json. Should be enough to configure the C# library
52
     *
53
     * @param Request $request
54
     * @param Response $response
55
     * @return Response
56
     */
57 1
    public function downloadJson(/** @scrutinizer ignore-unused */ Request $request, Response $response)
58
    {
59 1
        $jsonStart = "[";
60 1
        $jsonEnd = "]";
61
62 1
        $entities = $this->getDatabaseService()->getFromDatabase(new Comic(), null, null, "num");
63 1
        foreach ($entities as $entity) {
64 1
            $jsonStart .= $entity->json . ",";
65
        }
66
67 1
        return $this->returnRawJson($response, substr($jsonStart, 0, -1) . $jsonEnd);
68
    }
69
}
70