ActionResponse::hasAffectedRecords()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
final class ActionResponse implements ActionResponseInterface
8
{
9
    private $redirect;
10
    private $redirectParams;
11
    private $affected;
12
    private $errors;
13
14
    private function __construct()
15
    {
16
    }
17
18
    public static function create(array $options): ActionResponse
19
    {
20
        $defaults = [
21
            'redirect' => null,
22
            'redirectParams' => [],
23
            'errors' => [],
24
            'affected' => 0,
25
        ];
26
27 View Code Duplication
        if ($diff = array_diff(array_keys($options), array_keys($defaults))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
28
            throw new \InvalidArgumentException(sprintf(
29
                'Unexpected keys for action response: "%s", valid keys: "%s"',
30
                implode('", "', $diff), implode('", "', array_keys($defaults))
31
            ));
32
        }
33
34
        $options = array_merge($defaults, $options);
35
36
        if ($options['affected'] < 0) {
37
            throw new \InvalidArgumentException(sprintf(
38
                'Number of affected records cannot be negative (got "%s")',
39
                $options['affected']
40
            ));
41
        }
42
43
        $response = new self();
44
        $response->redirect = $options['redirect'];
45
        $response->redirectParams = $options['redirectParams'];
46
        $response->errors = $options['errors'];
47
        $response->affected = $options['affected'];
48
49
        return $response;
50
    }
51
52
    public function hasRedirect(): bool
53
    {
54
        return isset($this->redirect);
55
    }
56
57
    public function getRedirect(): string
58
    {
59
        return $this->redirect;
60
    }
61
62
    public function getRedirectParams(): array
63
    {
64
        return $this->redirectParams;
65
    }
66
67
    public function getErrors(): array
68
    {
69
        return $this->errors;
70
    }
71
72
    public function hasErrors(): bool
73
    {
74
        return false === empty($this->errors);
75
    }
76
77
    public function getAffectedRecordCount(): int
78
    {
79
        return $this->affected;
80
    }
81
82
    public function hasAffectedRecords(): bool
83
    {
84
        return $this->affected > 0;
85
    }
86
}
87