Completed
Branch dev (d5d70c)
by Raffael
11:00
created

Search::get()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Elasticsearch\Api\v1;
13
14
use Balloon\App\Api\Controller;
15
use Balloon\App\Elasticsearch\Elasticsearch;
16
use Balloon\Exception;
17
use Balloon\Filesystem\Node\AttributeDecorator;
18
use Micro\Http\Response;
19
use Psr\Log\LoggerInterface;
20
21 View Code Duplication
class Search extends Controller
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
{
23
    /**
24
     * Elasticsearch.
25
     *
26
     * @var Elasticsearch
27
     */
28
    protected $es;
29
30
    /**
31
     * Attribut decorator.
32
     *
33
     * @var AttributeDecorator
34
     */
35
    protected $decorator;
36
37
    /**
38
     * Logger.
39
     *
40
     * @var LoggerInterface
41
     */
42
    protected $logger;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param Elasticsearch      $es
48
     * @param AttributeDecorator $decorator
49
     * @param LoggerInterface    $logger
50
     */
51
    public function __construct(Elasticsearch $es, AttributeDecorator $decorator, LoggerInterface $logger)
52
    {
53
        $this->es = $es;
54
        $this->decorator = $decorator;
55
        $this->logger = $logger;
56
    }
57
58
    /**
59
     * @api {get} /api/v1/node/search Search
60
     * @apiVersion 1.0.0
61
     * @apiName getSearch
62
     * @apiGroup Node
63
     * @apiPermission none
64
     * @apiDescription Extended search query using elasticsearch
65
     * @apiUse _nodeAttributes
66
     *
67
     * @apiExample (cURL) example:
68
     * #Fulltext search and search for a name
69
     * curl -XGET -H 'Content-Type: application/json' "https://SERVER/api/v2/node/search?pretty" -d '{
70
     *           "body": {
71
     *               "query": {
72
     *                   "bool": {
73
     *                       "should": [
74
     *                           {
75
     *                               "match": {
76
     *                                   "content": "house"
77
     *                               }
78
     *                           },
79
     *                           {
80
     *                               "match": {
81
     *                                   "name": "file.txt"
82
     *                               }
83
     *                           }
84
     *                       ]
85
     *                   }
86
     *               }
87
     *           }
88
     *       }'
89
     *
90
     * @apiParam (GET Parameter) {object} query Elasticsearch query object
91
     * @apiParam (GET Parameter) {string[]} [attributes] Filter node attributes
92
     * @apiParam (GET Parameter) {number} [deleted=0] Wherever include deleted nodes or not, possible values:</br>
93
     * - 0 Exclude deleted</br>
94
     * - 1 Only deleted</br>
95
     * - 2 Include deleted</br>
96
     *
97
     * @apiSuccess (200 OK) {object[]} data Node list (matched nodes)
98
     * @apiSuccessExample {json} Success-Response:
99
     * HTTP/1.1 200 OK
100
     * {
101
     *      "status":200,
102
     *      "data": [{...}, {...}]
103
     *      }
104
     * }
105
     *
106
     * @param array $query
107
     * @param array $attributes
108
     * @param int   $deleted
109
     *
110
     * @return Response
111
     */
112
    public function get(array $query, array $attributes = [], int $deleted = 0): Response
113
    {
114
        $children = [];
115
        $nodes = $this->es->search($query, $deleted);
116
117
        foreach ($nodes as $node) {
118
            try {
119
                $child = $this->decorator->decorate($node, $attributes);
120
                $children[] = $child;
121
            } catch (\Exception $e) {
122
                $this->logger->info('error occured during loading attributes, skip search result node', [
123
                    'category' => get_class($this),
124
                    'exception' => $e,
125
                ]);
126
            }
127
        }
128
129
        return (new Response())->setCode(200)->setBody($children);
130
    }
131
}
132