Completed
Push — master ( 3742bc...55196a )
by Julito
13:21
created

AbstractResourceController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 75
rs 10
c 1
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A trans() 0 3 1
A getRepository() 0 23 2
A getRepositoryFromRequest() 0 6 1
A __construct() 0 13 1
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Controller;
5
6
use Chamilo\CoreBundle\Block\BreadcrumbBlockService;
7
use Chamilo\CoreBundle\Repository\ResourceRepository;
8
use Chamilo\CoreBundle\ToolChain;
9
use Cocur\Slugify\SlugifyInterface;
10
use League\Flysystem\MountManager;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Contracts\Translation\TranslatorInterface;
13
14
/**
15
 * Class AbstractResourceController
16
 */
17
abstract class AbstractResourceController extends BaseController
18
{
19
    protected $mountManager;
20
    protected $toolChain;
21
    protected $translator;
22
    protected $breadcrumbBlockService;
23
    protected $fs;
24
    protected $slugify;
25
26
    public function __construct(
27
        MountManager $mountManager,
28
        ToolChain $toolChain,
29
        TranslatorInterface $translator,
30
        BreadcrumbBlockService $breadcrumbBlockService,
31
        SlugifyInterface $slugify
32
    ) {
33
        $this->mountManager = $mountManager;
34
        $this->fs = $mountManager->getFilesystem('resources_fs');
35
        $this->translator = $translator;
36
        $this->toolChain = $toolChain;
37
        $this->breadcrumbBlockService = $breadcrumbBlockService;
38
        $this->slugify = $slugify;
39
    }
40
41
    /**
42
     * @param string $variable
43
     *
44
     * @return string
45
     */
46
    public function trans($variable)
47
    {
48
        return $this->translator->trans($variable);
49
    }
50
51
    /**
52
     * @param Request $request
53
     *
54
     * @return ResourceRepository
55
     */
56
    public function getRepositoryFromRequest(Request $request)
57
    {
58
        $tool = $request->get('tool');
59
        $type = $request->get('type');
60
61
        return $this->getRepository($tool, $type);
62
    }
63
64
    /**
65
     * @param string $type
66
     *
67
     * @return ResourceRepository
68
     */
69
    public function getRepository($tool, $type)
70
    {
71
        $checker = $this->container->get('security.authorization_checker');
72
        $tool = $this->toolChain->getToolFromName($tool);
73
74
        $types = $tool->getTypes();
75
        if (!in_array($type, $types)) {
76
            return null;
77
        }
78
79
        $types = array_flip($types);
80
        $type = $types[$type];
81
82
        $resourceRepository = new ResourceRepository(
83
            $checker,
84
            $this->getDoctrine()->getManager(),
85
            $this->mountManager,
86
            $this->get('router'),
87
            $this->slugify,
88
            $type
89
        );
90
91
        return $resourceRepository;
92
    }
93
}
94