Completed
Push — master ( 482c38...561abf )
by Rafał
27:48 queued 18:15
created

AuthorController::findOr404()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
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 Content Bundle.
7
 *
8
 * Copyright 2020 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 2020 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\Controller;
18
19
use Nelmio\ApiDocBundle\Annotation\Operation;
20
use Swagger\Annotations as SWG;
21
use SWP\Bundle\ContentBundle\Model\ArticleAuthorInterface;
22
use SWP\Component\Common\Response\ResponseContext;
23
use SWP\Component\Common\Response\SingleResourceResponse;
24
use SWP\Component\Common\Response\SingleResourceResponseInterface;
25
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
26
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
27
use Symfony\Component\Routing\Annotation\Route;
28
29
class AuthorController extends AbstractController
30
{
31
    /**
32
     * @Operation(
33
     *     tags={"authors"},
34
     *     summary="Delete author",
35
     *     @SWG\Response(
36
     *         response="204",
37
     *         description="Returned on success."
38
     *     )
39
     * )
40
     *
41
     * @Route("/api/{version}/authors/{id}", methods={"DELETE"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_core_delete_author", requirements={"id"="\d+"})
42
     */
43
    public function deleteAction(int $id): SingleResourceResponseInterface
44
    {
45
        $authorRepository = $this->get('swp.repository.author');
46
        $author = $this->findOr404($id);
47
48
        $authorRepository->remove($author);
49
50
        return new SingleResourceResponse(null, new ResponseContext(204));
51
    }
52
53
    private function findOr404(int $id): ArticleAuthorInterface
54
    {
55
        if (null === $author = $this->get('swp.repository.author')->findOneById($id)) {
56
            throw new NotFoundHttpException('Author was not found.');
57
        }
58
59
        return $author;
60
    }
61
}
62