Completed
Push — master ( bc414b...6634bd )
by Paweł
10:09
created

SlideshowController::findOr404()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Core Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. 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 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\CoreBundle\Controller;
16
17
use Nelmio\ApiDocBundle\Annotation\Model;
18
use Nelmio\ApiDocBundle\Annotation\Operation;
19
use Swagger\Annotations as SWG;
20
use SWP\Component\Common\Response\ResourcesListResponseInterface;
21
use SWP\Component\Common\Response\SingleResourceResponseInterface;
22
use Symfony\Component\Routing\Annotation\Route;
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\SingleResourceResponse;
27
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
30
31
class SlideshowController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
32
{
33
    /**
34
     * @Operation(
35
     *     tags={"slideshow"},
36
     *     summary="List all slideshows",
37
     *     @SWG\Parameter(
38
     *         name="sorting",
39
     *         in="query",
40
     *         description="todo",
41
     *         required=false,
42
     *         type="string"
43
     *     ),
44
     *     @SWG\Response(
45
     *         response="200",
46
     *         description="Returned on success.",
47
     *         @SWG\Schema(
48
     *             type="array",
49
     *             @SWG\Items(ref=@Model(type=\SWP\Bundle\CoreBundle\Model\Slideshow::class, groups={"api"}))
50
     *         )
51
     *     )
52
     * )
53
     *
54
     * @Route("/api/{version}/content/slideshows/{articleId}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_slideshows_list")
55
     */
56
    public function listAction(Request $request, string $articleId): ResourcesListResponseInterface
57
    {
58
        $repository = $this->get('swp.repository.slideshow');
59
60
        $article = $this->findArticleOr404($articleId);
61
62
        $slideshows = $repository->getPaginatedByCriteria(new Criteria([
63
            'article' => $article,
64
        ]), $request->query->get('sorting', []), new PaginationData($request));
65
66
        return new ResourcesListResponse($slideshows);
67
    }
68
69
    /**
70
     * @Operation(
71
     *     tags={"slideshow"},
72
     *     summary="Get single slideshow",
73
     *     @SWG\Response(
74
     *         response="200",
75
     *         description="Returned on success.",
76
     *         @Model(type=\SWP\Bundle\CoreBundle\Model\Slideshow::class, groups={"api"})
77
     *     )
78
     * )
79
     *
80
     * @Route("/api/{version}/content/slideshows/{articleId}/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_get_slideshow", requirements={"id"="\d+"})
81
     */
82
    public function getAction($id, string $articleId): SingleResourceResponseInterface
83
    {
84
        $article = $this->findArticleOr404($articleId);
85
86
        if (null === $list = $this->get('swp.repository.slideshow')->findOneBy([
87
                'id' => $id,
88
                'article' => $article,
89
            ])) {
90
            throw new NotFoundHttpException(sprintf('Slideshow with id "%s" was not found.', $id));
91
        }
92
93
        return new SingleResourceResponse($list);
94
    }
95
96
    private function findArticleOr404($id)
97
    {
98
        if (null === $article = $this->get('swp.repository.article')->findOneById($id)) {
99
            throw new NotFoundHttpException(sprintf('Article with id "%s" was not found.', $id));
100
        }
101
102
        return $article;
103
    }
104
}
105