Completed
Pull Request — 1.5 (#773)
by Paweł
10:28
created

ArticleCommentsController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 0
loc 62
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B updateAction() 0 37 6
A getFragmentFromUrl() 0 9 2
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 function array_key_exists;
20
use function parse_url;
21
use function str_replace;
22
use function strpos;
23
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
24
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
25
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
26
use SWP\Bundle\ContentBundle\ArticleEvents;
27
use SWP\Bundle\ContentBundle\Event\ArticleEvent;
28
use SWP\Bundle\ContentBundle\Form\Type\ArticleCommentsType;
29
use SWP\Component\Common\Exception\NotFoundHttpException;
30
use SWP\Component\Common\Response\ResponseContext;
31
use SWP\Component\Common\Response\SingleResourceResponse;
32
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
33
use Symfony\Component\HttpFoundation\Request;
34
35
class ArticleCommentsController extends Controller
36
{
37
    /**
38
     * @ApiDoc(
39
     *     resource=true,
40
     *     description="Update article comments number",
41
     *     statusCodes={
42
     *         200="Returned on success.",
43
     *         404="Return when article was not found"
44
     *     },
45
     * )
46
     * @Route("/api/{version}/content/articles", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_article_comments")
47
     * @Method("PATCH")
48
     */
49
    public function updateAction(Request $request)
50
    {
51
        $repository = $this->get('swp.repository.article');
52
        $articleResolver = $this->container->get('swp.resolver.article');
53
54
        $form = $this->createForm(ArticleCommentsType::class, [], ['method' => $request->getMethod()]);
55
        $form->handleRequest($request);
56
        if ($form->isValid()) {
57
            $data = $form->getData();
58
            $article = null;
59
            if (null !== $data['url']) {
60
                $article = strpos($data['url'], '/r/') ? $repository->findOneBySlug(
61
                    str_replace('/r/', '', $this->getFragmentFromUrl($data['url'], 'path'))
62
                ) : $articleResolver->resolve($data['url']);
63
            } elseif (null !== $data['id']) {
64
                $article = $repository->findOneBy(['id' => $data['id']]);
65
            }
66
67
            if (null === $article) {
68
                throw new NotFoundHttpException('Article was not found');
69
            }
70
71
            $article->setCommentsCount((int) $data['commentsCount']);
72
            $article->cancelTimestampable(true);
73
            $repository->flush();
74
75
            $this->container->get('event_dispatcher')->dispatch(ArticleEvents::POST_UPDATE, new ArticleEvent(
76
                $article,
77
                $article->getPackage(),
78
                ArticleEvents::POST_UPDATE
79
            ));
80
81
            return new SingleResourceResponse($article);
82
        }
83
84
        return new SingleResourceResponse($form, new ResponseContext(400));
85
    }
86
87
    private function getFragmentFromUrl(string $url, string $fragment): ?string
88
    {
89
        $fragments = parse_url($url);
90
        if (!array_key_exists($fragment, $fragments)) {
91
            return null;
92
        }
93
94
        return $fragments[$fragment];
95
    }
96
}
97