PluginController::storeAction()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 20
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 5
nop 1
crap 20
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\Controller;
11
12
use AnimeDb\Bundle\AppBundle\Entity\Plugin;
13
use Doctrine\ORM\EntityRepository;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * Plugin.
19
 *
20
 * @author  Peter Gribanov <[email protected]>
21
 */
22
class PluginController extends BaseController
23
{
24
    /**
25
     * Cache lifetime 1 day.
26
     *
27
     * @var int
28
     */
29
    const CACHE_LIFETIME = 86400;
30
31
    /**
32
     * Installed plugins.
33
     *
34
     * @param Request $request
35
     *
36
     * @return Response
37
     */
38
    public function installedAction(Request $request)
39
    {
40
        $response = $this->getCacheTimeKeeper()->getResponse('AnimeDbAppBundle:Plugin');
41
        // response was not modified for this request
42
        if ($response->isNotModified($request)) {
43
            return $response;
44
        }
45
46
        /* @var $rep EntityRepository */
47
        $rep = $this->getDoctrine()->getRepository('AnimeDbAppBundle:Plugin');
48
49
        return $this->render('AnimeDbCatalogBundle:Plugin:installed.html.twig', [
50
            'plugins' => $rep->findAll(),
51
        ], $response);
52
    }
53
54
    /**
55
     * Store of plugins.
56
     *
57
     * @param Request $request
58
     *
59
     * @return Response
60
     */
61
    public function storeAction(Request $request)
62
    {
63
        $response = $this->getCacheTimeKeeper()->getResponse([], self::CACHE_LIFETIME);
64
        // response was not modified for this request
65
        if ($response->isNotModified($request)) {
66
            return $response;
67
        }
68
69
        $plugins = [];
70
        $data = $this->get('anime_db.api.client')->getPlugins();
71
        foreach ($data['plugins'] as $plugin) {
72
            $plugins[$plugin['name']] = $plugin;
73
            $plugins[$plugin['name']]['installed'] = false;
74
        }
75
76
        /* @var $rep EntityRepository */
77
        $rep = $this->getDoctrine()->getRepository('AnimeDbAppBundle:Plugin');
78
        /* @var $plugin Plugin */
79
        foreach ($rep->findAll() as $plugin) {
80
            $plugins[$plugin->getName()]['installed'] = true;
81
        }
82
83
        return $this->render('AnimeDbCatalogBundle:Plugin:store.html.twig', [
84
            'plugins' => $plugins,
85
        ], $response);
86
    }
87
}
88