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

AbstractResourceController::trans()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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