AdminScreenActionModule   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 56
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hook() 0 6 2
A doAdminInitAction() 0 13 3
A doCurrentScreenAction() 0 15 4
A ajaxActions() 0 3 1
1
<?php
2
3
namespace Leonidas\Framework\Module\Abstracts;
4
5
use Leonidas\Contracts\Extension\ModuleInterface;
6
use Leonidas\Hooks\TargetsAdminInitHook;
7
use Leonidas\Hooks\TargetsCurrentScreenHook;
8
use Psr\Http\Message\ServerRequestInterface;
9
use WebTheory\Saveyour\Http\Request;
10
use WP_Screen;
0 ignored issues
show
Bug introduced by
The type WP_Screen was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
abstract class AdminScreenActionModule extends Module implements ModuleInterface
13
{
14
    use TargetsAdminInitHook;
15
    use TargetsCurrentScreenHook;
16
17
    public function hook(): void
18
    {
19
        $this->targetCurrentScreenHook();
20
21
        if ($this->ajaxActions()) {
22
            $this->targetAdminInitHook();
23
        }
24
    }
25
26
    protected function doCurrentScreenAction(WP_Screen $screen): void
27
    {
28
        if (!in_array($screen->base, $this->bases())) {
29
            return;
30
        }
31
32
        foreach ($this->screens() as $property => $value) {
33
            if ($screen->{$property} !== $value) {
34
                return;
35
            }
36
        }
37
38
        $request = $this->getServerRequest()->withAttribute('screen', $screen);
39
40
        $this->action($request);
41
    }
42
43
    protected function doAdminInitAction(): void
44
    {
45
        if (true !== wp_doing_ajax()) {
0 ignored issues
show
Bug introduced by
The function wp_doing_ajax was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        if (true !== /** @scrutinizer ignore-call */ wp_doing_ajax()) {
Loading history...
46
            return;
47
        }
48
49
        $request = $this->getServerRequest();
50
51
        if (!in_array(Request::var($request, 'action'), $this->ajaxActions())) {
52
            return;
53
        }
54
55
        $this->action($request);
56
    }
57
58
    protected function ajaxActions(): array
59
    {
60
        return [];
61
    }
62
63
    abstract protected function action(ServerRequestInterface $request): void;
64
65
    abstract protected function screens(): array;
66
67
    abstract protected function bases(): array;
68
}
69