Passed
Push — master ( 501fc3...f64e27 )
by Paweł
47:53
created

RuleController::createAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Content Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\CoreBundle\Controller;
16
17
use FOS\RestBundle\Controller\FOSRestController;
18
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
22
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
23
use SWP\Component\Common\Response\ResourcesListResponse;
24
use SWP\Component\Common\Response\ResponseContext;
25
use SWP\Component\Common\Response\SingleResourceResponse;
26
use SWP\Component\Common\Criteria\Criteria;
27
use SWP\Component\Common\Pagination\PaginationData;
28
use SWP\Bundle\RuleBundle\Form\Type\RuleType;
29
use SWP\Component\Rule\Model\RuleInterface;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
32
33
class RuleController extends FOSRestController
34
{
35
    /**
36
     * List all Rule entities for current tenant.
37
     *
38
     * @ApiDoc(
39
     *     resource=true,
40
     *     description="List all rules",
41
     *     statusCodes={
42
     *         200="Returned on success.",
43
     *         405="Method Not Allowed."
44
     *     }
45
     * )
46
     * @Route("/api/{version}/rules/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_list_rule")
47
     * @Method("GET")
48
     *
49
     * @Cache(expires="10 minutes", public=true)
50
     */
51 1
    public function listAction(Request $request)
52
    {
53 1
        $rules = $this->get('swp.repository.rule')
54 1
            ->getPaginatedByCriteria(new Criteria(), [], new PaginationData($request));
55
56 1
        if (0 === $rules->count()) {
57
            throw new NotFoundHttpException('No rules were found.');
58
        }
59
60 1
        return new ResourcesListResponse($rules);
61
    }
62
63
    /**
64
     * Get single Rule Entity.
65
     *
66
     * @ApiDoc(
67
     *     resource=true,
68
     *     description="Get single rule",
69
     *     statusCodes={
70
     *         200="Returned on success.",
71
     *         404="Rule not found.",
72
     *         405="Method Not Allowed."
73
     *     }
74
     * )
75
     * @Route("/api/{version}/rules/{id}", requirements={"id"="\d+"}, options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_get_rule")
76
     * @Method("GET")
77
     * @ParamConverter("rule", class="SWP\Bundle\CoreBundle\Model\Rule")
78
     *
79
     * @Cache(expires="10 minutes", public=true)
80
     */
81 1
    public function getAction(RuleInterface $rule)
82
    {
83 1
        return new SingleResourceResponse($rule);
84
    }
85
86
    /**
87
     * Create new Rule.
88
     *
89
     * @ApiDoc(
90
     *     resource=true,
91
     *     description="Create new rule",
92
     *     statusCodes={
93
     *         201="Returned on success.",
94
     *         400="Returned on validation error.",
95
     *         405="Method Not Allowed."
96
     *     },
97
     *     input="SWP\Bundle\RuleBundle\Form\Type\RuleType"
98
     * )
99
     * @Route("/api/{version}/rules/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_create_rule")
100
     * @Method("POST")
101
     */
102 7 View Code Duplication
    public function createAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104 7
        $ruleRepository = $this->get('swp.repository.rule');
105
106 7
        $rule = $this->get('swp.factory.rule')->create();
107 7
        $form = $this->createForm(RuleType::class, $rule);
108 7
        $form->handleRequest($request);
109
110 7
        if ($form->isValid()) {
111 7
            $ruleRepository->add($rule);
112
113 7
            return new SingleResourceResponse($rule, new ResponseContext(201));
114
        }
115
116
        return new SingleResourceResponse($form, new ResponseContext(400));
117
    }
118
119
    /**
120
     * Delete single rule.
121
     *
122
     * @ApiDoc(
123
     *     resource=true,
124
     *     description="Delete single rule",
125
     *     statusCodes={
126
     *         204="Returned on success.",
127
     *         404="Returned when rule not found.",
128
     *         405="Returned when method not allowed."
129
     *     }
130
     * )
131
     * @Route("/api/{version}/rules/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_delete_rule", requirements={"id"="\d+"})
132
     * @Method("DELETE")
133
     * @ParamConverter("rule", class="SWP\Bundle\CoreBundle\Model\Rule")
134
     */
135 1
    public function deleteAction(RuleInterface $rule)
136
    {
137 1
        $ruleRepository = $this->get('swp.repository.rule');
138 1
        $ruleRepository->remove($rule);
139
140 1
        return new SingleResourceResponse(null, new ResponseContext(204));
141
    }
142
143
    /**
144
     * Updates single rule.
145
     *
146
     * @ApiDoc(
147
     *     resource=true,
148
     *     description="Update single rule",
149
     *     statusCodes={
150
     *         201="Returned on success.",
151
     *         400="Returned on validation error.",
152
     *         404="Rule not found.",
153
     *         405="Method Not Allowed."
154
     *     },
155
     *     input="SWP\Bundle\RuleBundle\Form\Type\RuleType"
156
     * )
157
     * @Route("/api/{version}/rules/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_update_rule", requirements={"id"="\d+"})
158
     * @Method("PATCH")
159
     * @ParamConverter("rule", class="SWP\Bundle\CoreBundle\Model\Rule")
160
     */
161 2 View Code Duplication
    public function updateAction(Request $request, RuleInterface $rule)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
    {
163 2
        $objectManager = $this->get('swp.object_manager.rule');
164
165 2
        $form = $this->createForm(RuleType::class, $rule, [
166 2
            'method' => $request->getMethod(),
167
        ]);
168
169 2
        $form->handleRequest($request);
170 2
        if ($form->isValid()) {
171 2
            $objectManager->flush();
172 2
            $objectManager->refresh($rule);
173
174 2
            return new SingleResourceResponse($rule);
175
        }
176
177
        return new SingleResourceResponse($form, new ResponseContext(400));
178
    }
179
}
180