Passed
Pull Request — master (#20)
by
unknown
07:09
created

ShippingExportController::getLabel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 2
nc 2
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A ShippingExportController::redirectToReferer() 0 8 2
1
<?php
2
3
/*
4
 * This file has been created by the developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusShippingExportPlugin\Controller;
14
15
use BitBag\SyliusShippingExportPlugin\Event\ExportShipmentEvent;
16
use BitBag\SyliusShippingExportPlugin\Repository\ShippingExportRepositoryInterface;
17
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Webmozart\Assert\Assert;
21
22
final class ShippingExportController extends ResourceController
23
{
24
    /** @var ShippingExportRepositoryInterface */
25
    protected $repository;
26
27
    public function exportAllNewShipmentsAction(Request $request): RedirectResponse
28
    {
29
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
30
31
        $shippingExports = $this->repository->findAllWithNewState();
32
33
        if (0 === count($shippingExports)) {
34
            $this->addFlash('error', 'bitbag.ui.no_new_shipments_to_export');
35
36
            return $this->redirectToReferer($request);
37
        }
38
39
        foreach ($shippingExports as $shippingExport) {
40
            $this->eventDispatcher->dispatch(
41
                ExportShipmentEvent::SHORT_NAME,
42
                $configuration,
43
                $shippingExport
44
            );
45
        }
46
47
        return $this->redirectToReferer($request);
48
    }
49
50
    public function exportSingleShipmentAction(Request $request): RedirectResponse
51
    {
52
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
53
54
        $shippingExport = $this->repository->find($request->get('id'));
55
        Assert::notNull($shippingExport);
56
57
        $this->eventDispatcher->dispatch(
58
            ExportShipmentEvent::SHORT_NAME,
59
            $configuration,
60
            $shippingExport
0 ignored issues
show
Bug introduced by
It seems like $shippingExport can also be of type null; however, parameter $resource of Sylius\Bundle\ResourceBu...erInterface::dispatch() does only seem to accept Sylius\Component\Resource\Model\ResourceInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            /** @scrutinizer ignore-type */ $shippingExport
Loading history...
61
        );
62
63
        return $this->redirectToReferer($request);
64
    }
65
66
    private function redirectToReferer(Request $request): RedirectResponse
67
    {
68
        $referer = $request->headers->get('referer');
69
        if (null !== $referer) {
0 ignored issues
show
introduced by
The condition null !== $referer is always true.
Loading history...
70
            return new RedirectResponse($referer);
71
        }
72
73
        return $this->redirectToRoute($request->attributes->get('_route'));
74
    }
75
}
76