Completed
Pull Request — master (#277)
by Beñat
05:05
created

UsersByIdsAction::__invoke()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\IdentityAccess\Infrastructure\Symfony\HttpAction;
16
17
use Kreta\IdentityAccess\Application\Query\UsersOfIdsQuery;
18
use Kreta\IdentityAccess\Application\Query\UsersOfSearchStringQuery;
19
use Kreta\SharedKernel\Application\QueryBus;
20
use Symfony\Component\HttpFoundation\JsonResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
23
class UsersByIdsAction
24
{
25
    private $queryBus;
26
27
    public function __construct(QueryBus $queryBus)
28
    {
29
        $this->queryBus = $queryBus;
30
    }
31
32
    public function __invoke(Request $request) : JsonResponse
33
    {
34
35
        if ($request->query->get('ids')) {
36
          $ids = explode(',', $request->query->get('ids'));
37
          $this->queryBus->handle(
38
              new UsersOfIdsQuery($ids),
39
              $result
40
          );
41
42
          return new JsonResponse($result);
43
        }
44
45
        $search = $request->query->get('query');
46
        $excludedIds = $request->query->get('excludedIds');
47
48
        $this->queryBus->handle(
49
            new UsersOfSearchStringQuery($search, explode(',',$excludedIds)),
50
            $result
51
        );
52
53
        return new JsonResponse($result);
54
55
    }
56
}
57