Passed
Push — master ( 94b7c6...9cdec7 )
by Christian
15:16 queued 12s
created

PromotionController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 104
rs 10
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceIndividualCodes() 0 13 2
A generateIndividualCodes() 0 10 2
A generateFixedCode() 0 7 2
A __construct() 0 3 1
A getCodePreview() 0 7 2
A addIndividualCodes() 0 12 2
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, $codePattern, $amount, $context);
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/add-individual", name="api.action.promotion.codes.add-individual", methods={"POST"})
93
     * @Acl({"promotion.editor"})
94
     *
95
     * @throws NotFoundHttpException
96
     */
97
    public function addIndividualCodes(Request $request, Context $context): 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
        $promotionId = $request->request->get('promotionId');
104
        $amount = $request->request->getInt('amount');
105
106
        $this->codeService->addIndividualCodes($promotionId, $amount, $context);
107
108
        return new JsonResponse(null, Response::HTTP_NO_CONTENT);
109
    }
110
111
    /**
112
     * @Since("6.4.0.0")
113
     * @Route("/api/v{version}/_action/promotion/codes/preview", name="api.action.promotion.codes.preview", methods={"GET"})
114
     * @Acl({"promotion.editor"})
115
     *
116
     * @throws NotFoundHttpException
117
     */
118
    public function getCodePreview(Request $request): Response
119
    {
120
        if (!Feature::isActive('FEATURE_NEXT_12016')) {
121
            throw new NotFoundHttpException('Route not found, due to inactive flag FEATURE_NEXT_12016');
122
        }
123
124
        return new JsonResponse($this->codeService->getPreview($request->query->get('codePattern')));
125
    }
126
}
127