Completed
Push — master ( 586166...fecb59 )
by Paweł
47:58
created

RuleController::deleteAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
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 FOS\RestBundle\View\View;
19
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
22
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
23
use SWP\Bundle\RuleBundle\Form\Type\RuleType;
24
use SWP\Component\Rule\Model\RuleInterface;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
27
28
class RuleController extends FOSRestController
29
{
30
    /**
31
     * List all Rule entities for current tenant.
32
     *
33
     * @ApiDoc(
34
     *     resource=true,
35
     *     description="List all rules",
36
     *     statusCodes={
37
     *         200="Returned on success.",
38
     *         405="Method Not Allowed."
39
     *     }
40
     * )
41
     * @Route("/api/{version}/rules/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_list_rule")
42
     * @Method("GET")
43
     */
44 1 View Code Duplication
    public function listAction(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...
45
    {
46 1
        $rules = $this->get('swp.repository.rule')->findAll();
47 1
        $paginator = $this->get('knp_paginator');
48 1
        $rules = $paginator->paginate($rules);
49
50 1
        if (0 === count($rules)) {
51
            throw new NotFoundHttpException('No rules were found.');
52
        }
53
54 1
        return $this->handleView(View::create($this->get('swp_pagination_rep')->createRepresentation($rules, $request), 200));
55
    }
56
57
    /**
58
     * Get single Rule Entity.
59
     *
60
     * @ApiDoc(
61
     *     resource=true,
62
     *     description="Get single rule",
63
     *     statusCodes={
64
     *         200="Returned on success.",
65
     *         404="Rule not found.",
66
     *         405="Method Not Allowed."
67
     *     }
68
     * )
69
     * @Route("/api/{version}/rules/{id}", requirements={"id"="\d+"}, options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_get_rule")
70
     * @Method("GET")
71
     * @ParamConverter("rule", class="SWPCoreBundle:Rule")
72
     */
73 1
    public function getAction(RuleInterface $rule)
74
    {
75 1
        return $this->handleView(View::create($rule, 200));
76
    }
77
78
    /**
79
     * Create new Rule.
80
     *
81
     * @ApiDoc(
82
     *     resource=true,
83
     *     description="Create new rule",
84
     *     statusCodes={
85
     *         201="Returned on success.",
86
     *         400="Returned on validation error.",
87
     *         405="Method Not Allowed."
88
     *     },
89
     *     input="SWP\Bundle\RuleBundle\Form\Type\RuleType"
90
     * )
91
     * @Route("/api/{version}/rules/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_create_rule")
92
     * @Method("POST")
93
     */
94 4 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...
95
    {
96 4
        $ruleRepository = $this->get('swp.repository.rule');
97
98 4
        $rule = $this->get('swp.factory.rule')->create();
99 4
        $form = $this->createForm(RuleType::class, $rule);
100 4
        $form->handleRequest($request);
101
102 4
        if ($form->isValid()) {
103 4
            $ruleRepository->add($rule);
104
105 4
            return $this->handleView(View::create($rule, 201));
106
        }
107
108
        return $this->handleView(View::create($form, 400));
109
    }
110
111
    /**
112
     * Delete single rule.
113
     *
114
     * @ApiDoc(
115
     *     resource=true,
116
     *     description="Delete single rule",
117
     *     statusCodes={
118
     *         204="Returned on success.",
119
     *         404="Returned when rule not found.",
120
     *         405="Returned when method not allowed."
121
     *     }
122
     * )
123
     * @Route("/api/{version}/rules/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_delete_rule", requirements={"id"="\d+"})
124
     * @Method("DELETE")
125
     * @ParamConverter("rule", class="SWPCoreBundle:Rule")
126
     */
127 1
    public function deleteAction(RuleInterface $rule)
128
    {
129 1
        $ruleRepository = $this->get('swp.repository.rule');
130 1
        $ruleRepository->remove($rule);
131
132 1
        return $this->handleView(View::create(null, 204));
133
    }
134
135
    /**
136
     * Updates single rule.
137
     *
138
     * @ApiDoc(
139
     *     resource=true,
140
     *     description="Update single rule",
141
     *     statusCodes={
142
     *         201="Returned on success.",
143
     *         400="Returned on validation error.",
144
     *         404="Rule not found.",
145
     *         405="Method Not Allowed."
146
     *     },
147
     *     input="SWP\Bundle\RuleBundle\Form\Type\RuleType"
148
     * )
149
     * @Route("/api/{version}/rules/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_update_rule", requirements={"id"="\d+"})
150
     * @Method("PATCH")
151
     * @ParamConverter("rule", class="SWPCoreBundle:Rule")
152
     */
153 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...
154
    {
155 2
        $objectManager = $this->get('swp.object_manager.rule');
156
157 2
        $form = $this->createForm(RuleType::class, $rule, [
158 2
            'method' => $request->getMethod(),
159
        ]);
160
161 2
        $form->handleRequest($request);
162 2
        if ($form->isValid()) {
163 2
            $objectManager->flush();
164 2
            $objectManager->refresh($rule);
165
166 2
            return $this->handleView(View::create($rule, 200));
167
        }
168
169
        return $this->handleView(View::create($form, 400));
170
    }
171
}
172