Completed
Pull Request — master (#571)
by Rafał
12:29
created

PlanController::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Plan Bundle.
7
 *
8
 * Copyright 2018 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2018 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\PlanBundle\Controller;
18
19
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
22
use SWP\Bundle\PlanBundle\Form\Type\PlanType;
23
use SWP\Component\Common\Response\ResponseContext;
24
use SWP\Component\Common\Response\SingleResourceResponse;
25
use SWP\Component\Common\Response\SingleResourceResponseInterface;
26
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
27
use Symfony\Component\HttpFoundation\Request;
28
29
class PlanController extends Controller
30
{
31
    /**
32
     * Create a new Pricing Plan.
33
     *
34
     * @ApiDoc(
35
     *     resource=true,
36
     *     description="Create a new plan",
37
     *     statusCodes={
38
     *         201="Returned on success.",
39
     *         400="Returned on validation error.",
40
     *         405="Method Not Allowed."
41
     *     },
42
     *     input="SWP\Bundle\PlanBundle\Form\Type\PlanType"
43
     * )
44
     * @Route("/api/{version}/plans/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_plan_create")
45
     *
46
     * @Method("POST")
47
     *
48
     * @param Request $request
49
     *
50
     * @return SingleResourceResponseInterface
51
     */
52
    public function create(Request $request): SingleResourceResponseInterface
53
    {
54
        $planRepository = $this->get('swp.repository.plan');
55
        $planFactory = $this->get('swp.factory.plan');
56
        $formFactory = $this->get('form.factory');
57
58
        $plan = $planFactory->create();
59
        $form = $formFactory->create(PlanType::class, $plan);
60
61
        $form->handleRequest($request);
62
63
        if ($form->isValid()) {
64
            $planRepository->add($plan);
65
66
            return new SingleResourceResponse($plan, new ResponseContext(201));
67
        }
68
69
        return new SingleResourceResponse($form, new ResponseContext(400));
70
    }
71
}
72