Passed
Pull Request — master (#139)
by Matt
04:29
created

TagController   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 56
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A index() 0 43 1
1
<?php
2
3
namespace App\Controller;
4
5
use Elastica\Query;
6
use Elastica\Query\InnerHits;
7
use Elastica\Query\MultiMatch;
8
use Elastica\Query\Nested;
9
use FOS\ElasticaBundle\Finder\PaginatedFinderInterface;
10
use Knp\Component\Pager\PaginatorInterface;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
class TagController extends AbstractController
17
{
18
    /** @var array<string, array<int, string>> $translateParam */
19
    private static $translateParam = [
20
        'any' => ['images.tags', 'images.autoTags', 'images.textTags'],
21
        'tags' => ['images.tags'],
22
        'auto_tags' => ['images.autoTags'],
23
        'text_tags' => ['images.textTags'],
24
    ];
25
26
    /**
27
     * @Route("/tag/{tag}/{type}", name="tag", methods={"GET"}, requirements={"type": "any|tags|auto_tags|text_tags"})
28
     */
29
    public function index(
30
        string $tag,
31
        string $type = "any",
32
        Request $request,
33
        PaginatedFinderInterface $wanderFinder,
34
        PaginatorInterface $paginator
35
        ): Response
36
    {
37
        $fields = self::$translateParam[$type];
38
39
        $nmm = new MultiMatch();
40
        $nmm->setQuery($tag);
41
        // TODO By the looks of https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html
42
        // you might be able to just not setFields and it'll default to * and might catch everything
43
        // anyway.
44
        $nmm->setFields($fields);
45
46
        $nested = new Nested();
47
        $nested->setPath('images');
48
        $nested->setQuery($nmm);
49
50
        $innerHits = new InnerHits();
51
        // We want more than the default three inner hits, as there may be several related
52
        // images on a particular wander, but we don't want to bump things up too high otherwise
53
        // an overly-broad search will bring back way too many images even with pagination of
54
        // the outer results.
55
        $innerHits->setSize(10);
56
        $nested->setInnerHits($innerHits);
57
58
59
        $results = $wanderFinder->createHybridPaginatorAdapter($nested);
60
        $pagination = $paginator->paginate(
61
            $results,
62
            $request->query->getInt('page', 1),
63
            10 // TODO: Parameterise this results-per-page
64
        );
65
66
        return $this->render('tag/index.html.twig', [
67
            'tag' => $tag,
68
            // TODO: Take this back out; we'll only need to pass the pagination in the long run. I'm debugging.
69
            'results' => $results,
70
            'pagination' => $pagination,
71
            'search_type' => $type
72
        ]);
73
    }
74
}
75