Completed
Push — master ( 8d3dbf...7592af )
by Paweł
62:36
created

FbPageController::checkIfPageExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Core 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 Nelmio\ApiDocBundle\Annotation\ApiDoc;
18
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
20
use SWP\Bundle\CoreBundle\Form\Type\FacebookPageType;
21
use SWP\Bundle\CoreBundle\Model\FacebookPage;
22
use SWP\Bundle\FacebookInstantArticlesBundle\Model\PageInterface;
23
use SWP\Component\Common\Criteria\Criteria;
24
use SWP\Component\Common\Pagination\PaginationData;
25
use SWP\Component\Common\Response\ResourcesListResponse;
26
use SWP\Component\Common\Response\ResponseContext;
27
use SWP\Component\Common\Response\SingleResourceResponse;
28
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
31
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
32
33 View Code Duplication
class FbPageController extends Controller
0 ignored issues
show
Duplication introduced by
This class 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...
34
{
35
    /**
36
     * @ApiDoc(
37
     *     resource=true,
38
     *     description="Lists Facebook Pages",
39
     *     statusCodes={
40
     *         200="Returned on success.",
41
     *         500="Unexpected error."
42
     *     }
43
     * )
44
     * @Route("/api/{version}/facebook/pages/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_list_facebook_pages")
45
     * @Method("GET")
46
     */
47 1
    public function listAction(Request $request)
48
    {
49 1
        $repository = $this->get('swp.repository.facebook_page');
50
51 1
        $items = $repository->getPaginatedByCriteria(
52 1
            new Criteria(),
53 1
            ['id' => 'asc'],
54 1
            new PaginationData($request)
55
        );
56
57 1
        return new ResourcesListResponse($items);
58
    }
59
60
    /**
61
     * @ApiDoc(
62
     *     resource=true,
63
     *     description="Create Facebook page",
64
     *     statusCodes={
65
     *         201="Returned on success.",
66
     *         400="Returned when not valid data."
67
     *     },
68
     *     input="SWP\Bundle\CoreBundle\Form\Type\FacebookPageType"
69
     * )
70
     * @Route("/api/{version}/facebook/pages/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_create_facebook_pages")
71
     * @Method("POST")
72
     */
73 5
    public function createAction(Request $request)
74
    {
75
        /* @var FacebookPage $feed */
76 5
        $page = $this->get('swp.factory.facebook_page')->create();
77 5
        $form = $this->createForm(FacebookPageType::class, $page, ['method' => $request->getMethod()]);
78
79 5
        $form->handleRequest($request);
80 5
        if ($form->isValid()) {
81 5
            $this->checkIfPageExists($page);
82 5
            $this->get('swp.repository.facebook_page')->add($page);
83
84 5
            return new SingleResourceResponse($page, new ResponseContext(201));
85
        }
86
87
        return new SingleResourceResponse($form, new ResponseContext(400));
88
    }
89
90
    /**
91
     * @ApiDoc(
92
     *     resource=true,
93
     *     description="Delete Facebook page",
94
     *     statusCodes={
95
     *         204="Returned on success.",
96
     *         500="Unexpected error.",
97
     *         404="Page not found",
98
     *         409="Page is used by Instant Articles Feed"
99
     *     }
100
     * )
101
     * @Route("/api/{version}/facebook/pages/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_delete_facebook_pages")
102
     * @Method("DELETE")
103
     */
104 1
    public function deleteAction($id)
105
    {
106 1
        $repository = $this->get('swp.repository.facebook_page');
107 1
        if (null === $page = $this->get('swp.repository.facebook_page')->findOneBy(['id' => $id])) {
108
            throw new NotFoundHttpException('There is no Page with provided id!');
109
        }
110
111 1
        if (null !== $feed = $this->get('swp.repository.facebook_instant_articles_feed')->findOneBy(['facebookPage' => $id])) {
112
            throw new ConflictHttpException(sprintf('This Page is used by Instant Articles Feed with id: %s!', $feed->getId()));
113
        }
114
115 1
        $repository->remove($page);
116
117 1
        return new SingleResourceResponse(null, new ResponseContext(204));
118
    }
119
120
    /**
121
     * @param PageInterface $page
122
     */
123 5
    private function checkIfPageExists(PageInterface $page)
124
    {
125 5
        if (null !== $this->get('swp.repository.facebook_page')->findOneBy([
126 5
                'pageId' => $page->getPageId(),
127
            ])
128
        ) {
129 1
            throw new ConflictHttpException('This Page already exists!');
130
        }
131 5
    }
132
}
133