Completed
Push — master ( d01ad3...6b97e6 )
by Paweł
21s queued 10s
created

ArticleSearchController::searchAction()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 8.9381
c 0
b 0
f 0
cc 3
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher ElasticSearch Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. 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 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ElasticSearchBundle\Controller\Api;
18
19
use Nelmio\ApiDocBundle\Annotation\Operation;
20
use Swagger\Annotations as SWG;
21
use SWP\Bundle\ElasticSearchBundle\Criteria\Criteria;
22
use SWP\Bundle\ElasticSearchBundle\Repository\ArticleRepository;
23
use SWP\Component\Common\Response\ResourcesListResponse;
24
use SWP\Component\Common\Response\ResponseContext;
25
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\Routing\Annotation\Route;
28
29
class ArticleSearchController 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...
30
{
31
    /**
32
     * @Operation(
33
     *     tags={"article"},
34
     *     summary="Search articles",
35
     *     @SWG\Parameter(
36
     *         name="status",
37
     *         in="query",
38
     *         description="Package status",
39
     *         required=false,
40
     *         type="array",
41
     *         @SWG\Items(type="string")
42
     *     ),
43
     *     @SWG\Parameter(
44
     *         name="published_before",
45
     *         in="query",
46
     *         description="The datetime before which the article has been published",
47
     *         required=false,
48
     *         type="string"
49
     *     ),
50
     *     @SWG\Parameter(
51
     *         name="published_after",
52
     *         in="query",
53
     *         description="The datetime after which the article has been published",
54
     *         required=false,
55
     *         type="string"
56
     *     ),
57
     *     @SWG\Parameter(
58
     *         name="author",
59
     *         in="query",
60
     *         description="Article authors",
61
     *         required=false,
62
     *         type="array",
63
     *         @SWG\Items(type="string")
64
     *     ),
65
     *     @SWG\Parameter(
66
     *         name="term",
67
     *         in="query",
68
     *         description="Search phrase",
69
     *         required=false,
70
     *         type="string"
71
     *     ),
72
     *     @SWG\Parameter(
73
     *         name="sorting",
74
     *         in="query",
75
     *         description="List order",
76
     *         required=false,
77
     *         type="array",
78
     *         @SWG\Items(type="string")
79
     *     ),
80
     *     @SWG\Parameter(
81
     *         name="source",
82
     *         in="query",
83
     *         description="Sources",
84
     *         required=false,
85
     *         type="array",
86
     *         @SWG\Items(type="string")
87
     *     ),
88
     *     @SWG\Parameter(
89
     *         name="limit",
90
     *         in="query",
91
     *         description="Items per page",
92
     *         required=false,
93
     *         type="integer"
94
     *     ),
95
     *     @SWG\Parameter(
96
     *         name="page",
97
     *         in="query",
98
     *         description="Page number",
99
     *         required=false,
100
     *         type="integer"
101
     *     ),
102
     *     @SWG\Parameter(
103
     *         name="route",
104
     *         in="query",
105
     *         description="Routes ids",
106
     *         required=false,
107
     *         type="array",
108
     *         @SWG\Items(type="integer")
109
     *     ),
110
     *     @SWG\Parameter(
111
     *         name="metadata",
112
     *         in="query",
113
     *         description="Metadata (e.g. query param: ?metadata[located][]=Sydney&metadata[located][]=Berlin)",
114
     *         required=false,
115
     *         type="array",
116
     *         @SWG\Items(type="string")
117
     *     ),
118
     *     @SWG\Response(
119
     *         response="200",
120
     *         description="Returned on success."
121
     *     ),
122
     *     @SWG\Response(
123
     *         response="500",
124
     *         description="Unexpected error."
125
     *     )
126
     * )
127
     *
128
     * @Route("/api/{version}/content/articles/", methods={"GET"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_content_list_articles")
129
     */
130
    public function searchAction(Request $request)
131
    {
132
        $currentTenant = $this->get('swp_multi_tenancy.tenant_context')->getTenant();
133
134
        $criteria = Criteria::fromQueryParameters(
135
            $request->query->get('term', ''),
136
            [
137
                'page' => $request->query->get('page'),
138
                'sort' => $request->query->get('sorting'),
139
                'limit' => $request->query->get('limit', 10),
140
                'routes' => array_filter((array) $request->query->get('route', [])),
141
                'statuses' => array_filter((array) $request->query->get('status', [])),
142
                'authors' => array_filter((array) $request->query->get('author', [])),
143
                'publishedBefore' => $request->query->has('published_before') ? new \DateTime($request->query->get('published_before')) : null,
144
                'publishedAfter' => $request->query->has('published_after') ? new \DateTime($request->query->get('published_after')) : null,
145
                'publishedAt' => $request->query->get('published_at'),
146
                'tenantCode' => $currentTenant->getCode(),
147
                'sources' => array_filter((array) $request->query->get('source', [])),
148
                'metadata' => array_filter((array) $request->query->get('metadata', [])),
149
            ]
150
        );
151
152
        $extraFields = $this->get('service_container')->getParameter('env(ELASTICA_ARTICLE_EXTRA_FIELDS)');
153
154
        $options = [
155
            'sortNestedPath' => 'articleStatistics.pageViewsNumber',
156
        ];
157
158
        $repositoryManager = $this->get('fos_elastica.manager');
159
        /** @var ArticleRepository $repository */
160
        $repository = $repositoryManager->getRepository($this->getParameter('swp.model.article.class'));
161
        $articles = $repository->findByCriteria($criteria, json_decode($extraFields, true));
162
        $paginator = $this->get('knp_paginator');
163
        $pagination = $paginator->paginate(
164
            $articles,
165
            $request->query->get('page', 1),
166
            $criteria->getPagination()->getItemsPerPage(),
167
            $options
168
        );
169
170
        $responseContext = new ResponseContext();
171
        $responseContext->setSerializationGroups(
172
            [
173
                'Default',
174
                'api_articles_list',
175
                'api_articles_featuremedia',
176
                'api_article_media_list',
177
                'api_article_media_renditions',
178
                'api_image_details',
179
                'api_routes_list',
180
                'api_tenant_list',
181
                'api_articles_statistics_list',
182
            ]
183
        );
184
185
        return new ResourcesListResponse($pagination, $responseContext);
186
    }
187
}
188