Completed
Push — master ( e078e1...68c3a5 )
by Matt
23s queued 12s
created

TagController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 20
c 2
b 1
f 0
nc 1
nop 5
dl 0
loc 41
rs 9.6
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
        Request $request,
32
        PaginatedFinderInterface $wanderFinder,
33
        PaginatorInterface $paginator,
34
        string $type = "any"
35
    ): Response {
36
        $fields = self::$translateParam[$type];
37
38
        $nmm = new MultiMatch();
39
        $nmm->setQuery($tag);
40
        // TODO By the looks of https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html
41
        // you might be able to just not setFields and it'll default to * and might catch everything
42
        // anyway.
43
        $nmm->setFields($fields);
44
45
        $nested = new Nested();
46
        $nested->setPath('images');
47
        $nested->setQuery($nmm);
48
49
        $innerHits = new InnerHits();
50
        // We want more than the default three inner hits, as there may be several related
51
        // images on a particular wander, but we don't want to bump things up too high otherwise
52
        // an overly-broad search will bring back way too many images even with pagination of
53
        // the outer results.
54
        $innerHits->setSize(10);
55
        $nested->setInnerHits($innerHits);
56
57
        $results = $wanderFinder->createHybridPaginatorAdapter($nested);
58
        $pagination = $paginator->paginate(
59
            $results,
60
            $request->query->getInt('page', 1),
61
            10 // TODO: Parameterise this results-per-page
62
        );
63
64
        return $this->render('tag/index.html.twig', [
65
            'tag' => $tag,
66
            // TODO: Take this back out; we'll only need to pass the pagination in the long run. I'm debugging.
67
            'results' => $results,
68
            'pagination' => $pagination,
69
            'search_type' => $type
70
        ]);
71
    }
72
}
73