GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( e5413e...3eb92c )
by Mario
03:50
created

Export::__invoke()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.0648
c 0
b 0
f 0
cc 5
nc 3
nop 2
1
<?php
2
3
namespace Netgen\Bundle\InformationCollectionBundle\Controller\Admin\Export;
4
5
use eZ\Publish\Core\MVC\Symfony\Security\Authorization\Attribute;
6
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7
use Netgen\InformationCollection\API\Value\Export\ExportCriteria;
8
use Netgen\InformationCollection\API\Service\Exporter;
9
use Netgen\InformationCollection\Core\Export\ExportResponseFormatterRegistry;
10
use Symfony\Component\HttpFoundation\Request;
11
use Netgen\InformationCollection\Form\Type\ExportType;
12
use eZ\Publish\API\Repository\ContentService;
13
14
final class Export extends AbstractController
15
{
16
    /**
17
     * @var \eZ\Publish\API\Repository\ContentService
18
     */
19
    protected $contentService;
20
21
    /**
22
     * @var \Netgen\InformationCollection\API\Service\Exporter
23
     */
24
    protected $exporter;
25
26
    /**
27
     * @var \Netgen\InformationCollection\Core\Export\ExportResponseFormatterRegistry
28
     */
29
    protected $formatterRegistry;
30
31
    public function __construct(
32
        ContentService $contentService,
33
        Exporter $exporter,
34
        ExportResponseFormatterRegistry $formatterRegistry
35
    )
36
    {
37
        $this->contentService = $contentService;
38
        $this->exporter = $exporter;
39
        $this->formatterRegistry = $formatterRegistry;
40
    }
41
42
    public function __invoke($contentId, Request $request)
43
    {
44
        $attribute = new Attribute('infocollector', 'export');
45
        $this->denyAccessUnlessGranted($attribute);
46
47
        $content = $this->contentService->loadContent($contentId);
48
49
        $form = $this->createForm(ExportType::class, null, [
50
            'contentId' => $content->id,
51
        ]);
52
        $form->handleRequest($request);
53
54
        if ($form->get('cancel')->isClicked()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Form\FormInterface as the method isClicked() does only exist in the following implementations of said interface: Symfony\Component\Form\SubmitButton.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
55
            return $this->redirect($this->generateUrl('netgen_information_collection.route.admin.collection_list', ['contentId' => $contentId]));
56
        }
57
58
        if ($form->get('export')->isClicked() && $form->isSubmitted() && $form->isValid()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Form\FormInterface as the method isClicked() does only exist in the following implementations of said interface: Symfony\Component\Form\SubmitButton.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
59
60
            /** @var ExportCriteria $data */
61
            $data = $form->getData();
62
            $export = $this->exporter->export($data);
63
64
            $formatter = $this->formatterRegistry->getExportResponseFormatter($data->getExportIdentifier());
65
66
            return $formatter->format($export, $content);
67
        }
68
69
        return $this->render("@NetgenInformationCollection/admin/export_menu.html.twig",
70
            [
71
                'content' => $content,
72
                'form' => $form->createView(),
73
            ]
74
        );
75
    }
76
}
77