ElasticUserRepository::findUser()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 20
nc 2
nop 1
1
<?php
2
3
namespace Badger\Bundle\UserBundle\Doctrine\Repository;
4
5
use Badger\Component\User\Repository\ElasticUserRepositoryInterface;
6
use Elastica\SearchableInterface;
7
8
/**
9
 * User repository for data contained in the Elasticsearch database.
10
 *
11
 * @author  Olivier Soulet <[email protected]>
12
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
13
 */
14
class ElasticUserRepository implements ElasticUserRepositoryInterface
15
{
16
    /** @var SearchableInterface */
17
    protected $finder;
18
19
    /**
20
     * @param SearchableInterface $finder
21
     */
22
    public function __construct(SearchableInterface $finder)
23
    {
24
        $this->finder = $finder;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function findUser($token)
31
    {
32
        $query = new \Elastica\Query\Fuzzy();
33
        $query->setField('username', $token);
34
35
        $finalQuery = new \Elastica\Query($query);
36
        $finalQuery->setFields(['username', 'profilePicture']);
37
        $finalQuery->setHighlight(
38
            [
39
                'pre_tags'  => ['<em style="color: #FF66FF;">'],
40
                'post_tags' => ['</em>'],
41
                'fields'    =>
42
                    [
43
                        'username' =>
44
                            [
45
                                'fragment_size'       => 200,
46
                                'number_of_fragments' => 1,
47
                            ],
48
                    ]
49
            ]
50
        );
51
52
        $users = $this->finder->search($finalQuery);
53
54
        $results = [];
55
        foreach ($users as $user) {
56
            $results[] = [
57
                'username' => $user->getData()['username'][0],
58
                'profilePicture' => $user->getData()['profilePicture'][0],
59
                'highlights' => $user->getHighlights()['username'][0],
60
            ];
61
        }
62
63
        return $results;
64
    }
65
}
66