Passed
Push — master ( f5f62a...783e97 )
by Christian
12:34 queued 10s
created

PromotionController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 83
rs 10
c 1
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceIndividualCodes() 0 13 2
A generateIndividualCodes() 0 10 2
A getCodePreview() 0 7 2
A generateFixedCode() 0 7 2
A __construct() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Promotion\Api;
4
5
use Shopware\Core\Checkout\Promotion\Util\PromotionCodeService;
6
use Shopware\Core\Framework\Context;
7
use Shopware\Core\Framework\Feature;
8
use Shopware\Core\Framework\Routing\Annotation\Acl;
9
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
10
use Shopware\Core\Framework\Routing\Annotation\Since;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
use Symfony\Component\Routing\Annotation\Route;
17
18
/**
19
 * @RouteScope(scopes={"api"})
20
 */
21
class PromotionController extends AbstractController
22
{
23
    /**
24
     * @var PromotionCodeService
25
     */
26
    private $codeService;
27
28
    public function __construct(PromotionCodeService $codeService)
29
    {
30
        $this->codeService = $codeService;
31
    }
32
33
    /**
34
     * @Since("6.4.0.0")
35
     * @Route("/api/v{version}/_action/promotion/codes/generate-fixed", name="api.action.promotion.codes.generate-fixed", methods={"GET"})
36
     * @Acl({"promotion.editor"})
37
     *
38
     * @throws NotFoundHttpException
39
     */
40
    public function generateFixedCode(): Response
41
    {
42
        if (!Feature::isActive('FEATURE_NEXT_12016')) {
43
            throw new NotFoundHttpException('Route not found, due to inactive flag FEATURE_NEXT_12016');
44
        }
45
46
        return new JsonResponse($this->codeService->getFixedCode());
47
    }
48
49
    /**
50
     * @Since("6.4.0.0")
51
     * @Route("/api/v{version}/_action/promotion/codes/generate-individual", name="api.action.promotion.codes.generate-individual", methods={"GET"})
52
     * @Acl({"promotion.editor"})
53
     *
54
     * @throws NotFoundHttpException
55
     */
56
    public function generateIndividualCodes(Request $request): Response
57
    {
58
        if (!Feature::isActive('FEATURE_NEXT_12016')) {
59
            throw new NotFoundHttpException('Route not found, due to inactive flag FEATURE_NEXT_12016');
60
        }
61
62
        $codePattern = $request->query->get('codePattern');
63
        $amount = (int) $request->query->get('amount');
64
65
        return new JsonResponse($this->codeService->generateIndividualCodes($codePattern, $amount));
66
    }
67
68
    /**
69
     * @Since("6.4.0.0")
70
     * @Route("/api/v{version}/_action/promotion/codes/replace-individual", name="api.action.promotion.codes.replace-individual", methods={"PATCH"})
71
     * @Acl({"promotion.editor"})
72
     *
73
     * @throws NotFoundHttpException
74
     */
75
    public function replaceIndividualCodes(Request $request, Context $context): Response
76
    {
77
        if (!Feature::isActive('FEATURE_NEXT_12016')) {
78
            throw new NotFoundHttpException('Route not found, due to inactive flag FEATURE_NEXT_12016');
79
        }
80
81
        $promotionId = $request->request->get('promotionId');
82
        $codePattern = $request->request->get('codePattern');
83
        $amount = $request->request->get('amount');
84
85
        $this->codeService->replaceIndividualCodes($promotionId, $context, $codePattern, $amount);
86
87
        return new JsonResponse(null, Response::HTTP_NO_CONTENT);
88
    }
89
90
    /**
91
     * @Since("6.4.0.0")
92
     * @Route("/api/v{version}/_action/promotion/codes/preview", name="api.action.promotion.codes.preview", methods={"GET"})
93
     * @Acl({"promotion.editor"})
94
     *
95
     * @throws NotFoundHttpException
96
     */
97
    public function getCodePreview(Request $request): Response
98
    {
99
        if (!Feature::isActive('FEATURE_NEXT_12016')) {
100
            throw new NotFoundHttpException('Route not found, due to inactive flag FEATURE_NEXT_12016');
101
        }
102
103
        return new JsonResponse($this->codeService->getPreview($request->query->get('codePattern')));
104
    }
105
}
106