Completed
Push — 1.4 ( b01c64...288f4f )
by Paweł
17s queued 11s
created

ArticleCommentsController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 8
dl 0
loc 49
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A updateAction() 0 34 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2018 Sourcefabric z.u. 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\CoreBundle\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\ContentBundle\ArticleEvents;
23
use SWP\Bundle\ContentBundle\Event\ArticleEvent;
24
use SWP\Bundle\ContentBundle\Form\Type\ArticleCommentsType;
25
use SWP\Component\Common\Exception\NotFoundHttpException;
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
31
class ArticleCommentsController extends Controller
32
{
33
    /**
34
     * @ApiDoc(
35
     *     resource=true,
36
     *     description="Update article comments number",
37
     *     statusCodes={
38
     *         200="Returned on success.",
39
     *         404="Return when article was not found"
40
     *     },
41
     * )
42
     * @Route("/api/{version}/content/articles", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_article_comments")
43
     * @Method("PATCH")
44
     */
45
    public function updateAction(Request $request)
46
    {
47
        $repository = $this->get('swp.repository.article');
48
        $articleResolver = $this->container->get('swp.resolver.article');
49
50
        $form = $this->createForm(ArticleCommentsType::class, [], ['method' => $request->getMethod()]);
51
        $form->handleRequest($request);
52
        if ($form->isValid()) {
53
            $data = $form->getData();
54
            $article = null;
55
            if (null !== $data['url']) {
56
                $article = $articleResolver->resolve($data['url']);
57
            } elseif (null !== $data['id']) {
58
                $article = $repository->findOneBy(['id' => $data['id']]);
59
            }
60
61
            if (null === $article) {
62
                throw new NotFoundHttpException('Article was not found');
63
            }
64
65
            $article->setCommentsCount((int) $data['commentsCount']);
66
            $repository->flush();
67
68
            $this->container->get('event_dispatcher')->dispatch(ArticleEvents::POST_UPDATE, new ArticleEvent(
69
                $article,
70
                $article->getPackage(),
71
                ArticleEvents::POST_UPDATE
72
            ));
73
74
            return new SingleResourceResponse($article);
75
        }
76
77
        return new SingleResourceResponse($form, new ResponseContext(400));
78
    }
79
}
80