ActionSearchAbstract   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 87
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A __invoke() 0 11 1
search() 0 1 ?
getQuery() 0 1 ?
A action() 0 6 1
A getCursor() 0 12 3
1
<?php
2
namespace Staticus\Middlewares;
3
4
use Staticus\Acl\Roles;
5
use Staticus\Config\ConfigInterface;
6
use Staticus\Resources\Middlewares\PrepareResourceMiddlewareAbstract;
7
use Staticus\Resources\ResourceDOInterface;
8
use Zend\Diactoros\Response\EmptyResponse;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Staticus\Resources\File\ResourceDO;
12
use Zend\Diactoros\Response\JsonResponse;
13
use Staticus\Auth\UserInterface;
14
use Staticus\Auth\User;
15
16
abstract class ActionSearchAbstract extends MiddlewareAbstract
17
{
18
    const DEFAULT_CURSOR = 1;
19
20
    /**
21
     * @var ResourceDOInterface|ResourceDO
22
     */
23
    protected $resourceDO;
24
25
    /**
26
     * Search provider
27
     * @var mixed
28
     */
29
    protected $searcher;
30
31
    /**
32
     * @var UserInterface|User
33
     */
34
    protected $user;
35
36
    /**
37
     * @var ConfigInterface
38
     */
39
    protected $config;
40
41
    public function __construct(
42
        ResourceDOInterface $resourceDO
43
        , $generator
44
        , UserInterface $user
45
        , ConfigInterface $config
46
    )
47
    {
48
        $this->resourceDO = $resourceDO;
49
        $this->searcher = $generator;
50
        $this->user = $user;
51
        $this->config = $config;
52
    }
53
54
    /**
55
     * @param ServerRequestInterface $request
56
     * @param ResponseInterface $response
57
     * @param callable|null $next
58
     * @return EmptyResponse
59
     * @throws \Exception
60
     */
61
    public function __invoke(
62
        ServerRequestInterface $request,
63
        ResponseInterface $response,
64
        callable $next = null
65
    )
66
    {
67
        parent::__invoke($request, $response, $next);
68
        $this->response = $this->action();
69
70
        return $this->next();
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    abstract protected function search();
77
78
    abstract protected function getQuery();
79
80
    protected function action()
81
    {
82
        $response = $this->search();
83
84
        return new JsonResponse(['found' => $response]);
85
    }
86
87
    /**
88
     * @return int
89
     */
90
    protected function getCursor()
91
    {
92
        $allowCursor = $this->config->get('staticus.search.allow_cursor_for_users', false);
93
        $roles = $this->user->getRoles();
94
        if ($allowCursor || in_array(Roles::ADMIN, $roles, true)) {
95
            $cursor = (int)PrepareResourceMiddlewareAbstract::getParamFromRequest('cursor', $this->request);
96
97
            return $cursor;
98
        }
99
100
        return self::DEFAULT_CURSOR;
101
    }
102
}