exportAllNewShipmentsAction()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 21
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusShippingExportPlugin\Controller;
12
13
use BitBag\SyliusShippingExportPlugin\Event\ExportShipmentEvent;
14
use BitBag\SyliusShippingExportPlugin\Repository\ShippingExportRepositoryInterface;
15
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
16
use Sylius\Component\Resource\Model\ResourceInterface;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Webmozart\Assert\Assert;
20
21
final class ShippingExportController extends ResourceController
22
{
23
    /** @var ShippingExportRepositoryInterface */
24
    protected $repository;
25
26
    public function exportAllNewShipmentsAction(Request $request): RedirectResponse
27
    {
28
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
29
30
        $shippingExports = $this->repository->findAllWithNewOrPendingState();
31
32
        if (0 === count($shippingExports)) {
33
            $this->addFlash('error', 'bitbag.ui.no_new_shipments_to_export');
34
35
            return $this->redirectToReferer($request);
36
        }
37
38
        foreach ($shippingExports as $shippingExport) {
39
            $this->eventDispatcher->dispatch(
40
                ExportShipmentEvent::SHORT_NAME,
41
                $configuration,
42
                $shippingExport
43
            );
44
        }
45
46
        return $this->redirectToReferer($request);
47
    }
48
49
    public function exportSingleShipmentAction(Request $request): RedirectResponse
50
    {
51
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
52
53
        /** @var ResourceInterface|null $shippingExport */
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