Passed
Push — master ( 3f7be1...ce932b )
by Matt
04:03
created

TagController::index()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 7
eloc 25
c 3
b 1
f 0
nc 8
nop 5
dl 0
loc 47
rs 8.5866
1
<?php
2
3
namespace App\Controller;
4
5
use Elastica\Query;
6
use Elastica\Query\BoolQuery;
7
use Elastica\Query\InnerHits;
8
use Elastica\Query\MultiMatch;
9
use Elastica\Query\Nested;
10
use Elastica\Query\Term;
11
use FOS\ElasticaBundle\Finder\PaginatedFinderInterface;
12
use Knp\Component\Pager\PaginatorInterface;
13
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\Routing\Annotation\Route;
17
18
class TagController extends AbstractController
19
{
20
    /** @var array<string> $translateParam */
21
    private static $translateParam = [
22
        'any'           => 'all tag types',
23
        'hand-tag'      => 'only hand-created tags',
24
        'auto-tag'      => 'only tags created by automatic image recognition',
25
        'text-tag'      => 'only tags created by automatic text recognition'
26
    ];
27
28
    /**
29
     * @Route("/tag/{tag}/{type}", name="tag", methods={"GET"}, requirements={"type": "any|hand-tag|auto-tag|text-tag"})
30
     */
31
    public function index(
32
        string $tag,
33
        Request $request,
34
        PaginatedFinderInterface $wanderFinder,
35
        PaginatorInterface $paginator,
36
        string $type = "any"
37
    ): Response {
38
        $boolQuery = new BoolQuery();
39
        if ($type === 'hand-tag' || $type === 'any') {
40
            $boolQuery->addShould(new Term(['images.slugifiedTags' => ['value' => $tag]]));
41
        }
42
        if ($type === 'auto-tag' || $type === 'any') {
43
            $boolQuery->addShould(new Term(['images.slugifiedAutoTags' => ['value' => $tag]]));
44
        }
45
        if ($type === 'text-tag' || $type === 'any') {
46
            $boolQuery->addShould(new Term(['images.slugifiedTextTags' => ['value' => $tag]]));
47
        }
48
49
        $boolQuery->setMinimumShouldMatch(1);
50
51
        $nested = new Nested();
52
        $nested->setPath('images');
53
        $nested->setQuery($boolQuery);
54
55
        $innerHits = new InnerHits();
56
        // We want more than the default three inner hits, as there may be several related
57
        // images on a particular wander, but we don't want to bump things up too high otherwise
58
        // an overly-broad search will bring back way too many images even with pagination of
59
        // the outer results.
60
        $innerHits->setSize(10);
61
        $nested->setInnerHits($innerHits);
62
63
        $searchDescription = self::$translateParam[$type];
64
65
        $results = $wanderFinder->createHybridPaginatorAdapter($nested);
66
        $pagination = $paginator->paginate(
67
            $results,
68
            $request->query->getInt('page', 1),
69
            10 // TODO: Parameterise this results-per-page
70
        );
71
72
        return $this->render('tag/index.html.twig', [
73
            'tag' => $tag,
74
            // TODO: Take this back out; we'll only need to pass the pagination in the long run. I'm debugging.
75
            'results' => $results,
76
            'pagination' => $pagination,
77
            'search_description' => $searchDescription
78
        ]);
79
    }
80
}
81