Completed
Push — master ( 13dd2e...a1a590 )
by Julito
20:58
created

onPreSerialize()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
eloc 26
c 2
b 0
f 0
nc 12
nop 1
dl 0
loc 44
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\EventSubscriber;
6
7
use ApiPlatform\Core\EventListener\EventPriorities;
8
use ApiPlatform\Core\Util\RequestAttributesExtractor;
9
use Chamilo\CoreBundle\Entity\AbstractResource;
10
use Chamilo\CoreBundle\Repository\ResourceNodeRepository;
11
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpKernel\Event\ViewEvent;
14
use Symfony\Component\HttpKernel\KernelEvents;
15
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16
17
class ResolveResourceFileContentUrlSubscriber implements EventSubscriberInterface
18
{
19
    private $generator;
20
    private $resourceNodeRepository;
21
22
    public function __construct(UrlGeneratorInterface $generator, ResourceNodeRepository $resourceNodeRepository)
23
    {
24
        $this->generator = $generator;
25
        $this->resourceNodeRepository = $resourceNodeRepository;
26
    }
27
28
    public static function getSubscribedEvents(): array
29
    {
30
        return [
31
            KernelEvents::VIEW => ['onPreSerialize', EventPriorities::PRE_SERIALIZE],
32
        ];
33
    }
34
35
    public function onPreSerialize(ViewEvent $event): void
36
    {
37
        $controllerResult = $event->getControllerResult();
38
        $request = $event->getRequest();
39
40
        if ($controllerResult instanceof Response || !$request->attributes->getBoolean('_api_respond', true)) {
41
            return;
42
        }
43
        $attributes = RequestAttributesExtractor::extractAttributes($request);
44
45
        if (!($attributes = RequestAttributesExtractor::extractAttributes($request)) ||
46
            //!\is_a($attributes['resource_class'], ResourceFile::class, true)
47
            !\is_a($attributes['resource_class'], AbstractResource::class, true)
48
        ) {
49
            return;
50
        }
51
        $mediaObjects = $controllerResult;
52
53
        if (!is_iterable($mediaObjects)) {
54
            $mediaObjects = [$mediaObjects];
55
        }
56
        //error_log($request->get('getFile'));
57
        $getFile = $request->get('getFile');
58
        //$getFile = true;
59
        foreach ($mediaObjects as $mediaObject) {
60
            if (!$mediaObject instanceof AbstractResource) {
61
                continue;
62
            }
63
            if ($mediaObject->hasResourceNode()) {
64
                $resourceNode = $mediaObject->getResourceNode();
65
66
                $params = [
67
                    'id' => $resourceNode->getId(),
68
                    'tool' => $resourceNode->getResourceType()->getTool()->getName(),
69
                    'type' => $resourceNode->getResourceType()->getName(),
70
                ];
71
72
                $mediaObject->contentUrl = $this->generator->generate('chamilo_core_resource_view_file', $params);
73
74
                if ($getFile &&
75
                    $resourceNode->hasResourceFile() &&
76
                    $resourceNode->hasEditableContent()
77
                ) {
78
                    $mediaObject->contentFile = $this->resourceNodeRepository->getResourceNodeFileContent($resourceNode);
79
                }
80
            }
81
        }
82
    }
83
}
84