DownloadsController::provider()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 2
b 0
f 0
nc 4
nop 3
dl 0
loc 18
rs 9.9666
1
<?php
2
3
namespace App\Controller\Api;
4
5
use App\Document\Provider;
6
use Doctrine\ODM\MongoDB\DocumentManager;
7
use Nelmio\ApiDocBundle\Annotation\Security;
8
use Swagger\Annotations as SWG;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
/**
14
 * Provide routes for stats API.
15
 *
16
 * @Route("/api/downloads", name="api_downloads_", defaults={"_format": "json"})
17
 */
18
class DownloadsController extends AbstractController
19
{
20
    /**
21
     * Profile.
22
     *
23
     * @Route("/{name}/{version}", name="provider", methods={"GET"}, requirements={"name": ".+"})
24
     * @SWG\Response(
25
     *     response=200,
26
     *     description="Returns provider downloads",
27
     * )
28
     * @SWG\Tag(name="downloads")
29
     * @Security(name="Bearer")
30
     */
31
    public function provider(Provider $provider, $version, DocumentManager $dm): Response
32
    {
33
        $all = $dm->getRepository('App:Download')->getProviderStats($provider);
34
        $version = $dm->getRepository('App:Download')->getProviderStats($provider, $version);
35
36
        $data = [];
37
38
        foreach ($all as $item) {
39
            $data[$item['date']]['all'] = $item['downloads'];
40
        }
41
42
        foreach ($version as $item) {
43
            $data[$item['date']]['version'] = $item['downloads'];
44
        }
45
46
        ksort($data);
47
48
        return $this->json($data);
49
    }
50
}
51