Completed
Push — master ( 3a5b5f...667dde )
by Julito
15:02
created

ResourceFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
nc 1
nop 6
dl 0
loc 15
rs 10
c 1
b 0
f 1
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Repository;
5
6
use Chamilo\CoreBundle\Block\BreadcrumbBlockService;
7
use Chamilo\CoreBundle\ToolChain;
8
use Cocur\Slugify\SlugifyInterface;
9
use Doctrine\ORM\EntityManagerInterface;
10
use League\Flysystem\MountManager;
11
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
12
use Symfony\Component\Routing\RouterInterface;
13
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
14
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
15
16
class ResourceFactory
17
{
18
    protected $mountManager;
19
    protected $toolChain;
20
    protected $fs;
21
    protected $slugify;
22
    protected $entityManager;
23
    protected $authorizationChecker;
24
25
    public function __construct(
26
        MountManager $mountManager,
27
        ToolChain $toolChain,
28
        SlugifyInterface $slugify,
29
        AuthorizationCheckerInterface $authorizationChecker,
30
        EntityManagerInterface $entityManager,
31
        RouterInterface $router
32
    ) {
33
        $this->mountManager = $mountManager;
34
        $this->fs = $mountManager->getFilesystem('resources_fs');
35
        $this->toolChain = $toolChain;
36
        $this->slugify = $slugify;
37
        $this->entityManager = $entityManager;
38
        $this->router = $router;
0 ignored issues
show
Bug Best Practice introduced by
The property router does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
39
        $this->authorizationChecker = $authorizationChecker;
40
    }
41
42
    public function createRepository(string $tool, string $type): ResourceRepository
43
    {
44
        $tool = $this->toolChain->getToolFromName($tool);
45
        $resourceTypeList = $tool->getResourceTypes();
46
        //$this->fs = $mountManager->getFilesystem('resources_fs');
47
48
        if (!isset($resourceTypeList[$type])) {
49
            throw new InvalidArgumentException("Resource type doesn't exist: $type");
50
        }
51
52
        $type = $resourceTypeList[$type];
53
        $repo = $type['repository'];
54
        $entity = $type['entity'];
55
56
        if (class_exists($repo) === false || class_exists($entity) === false) {
57
            throw new InvalidArgumentException("Check the configuration of the type: $type");
58
        }
59
60
        return new $repo(
61
            $this->authorizationChecker,
62
            $this->entityManager,
63
            $this->mountManager,
64
            $this->router,
65
            $this->slugify,
66
            $entity
67
        );
68
    }
69
}
70