MailchimpResponse::isSuccess()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 3
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 4
rs 10
1
<?php
2
3
4
namespace MailMarketing\Entities;
5
6
use Illuminate\Support\Arr;
7
use MailMarketing\MailMarketingException;
8
9
class MailchimpResponse implements ResponseInterface
10
{
11
    protected array $response = [];
12
13
    /**
14
     * @throws MailMarketingException
15
     */
16 10
    public function __construct($response = [])
17
    {
18 10
        if (is_bool($response)) {
19 8
            $response = [
20 8
                'status' => $response,
21 8
            ];
22
        }
23 10
        if (!is_array($response)) {
24 1
            throw new MailMarketingException('Mailchimp response not array: ' . print_r($response, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($response, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

24
            throw new MailMarketingException('Mailchimp response not array: ' . /** @scrutinizer ignore-type */ print_r($response, true));
Loading history...
25
        }
26
27 9
        $this->response = $response;
28
    }
29
30
    /**
31
     * @return array
32
     */
33 2
    public function toArray(): array
34
    {
35 2
        return $this->getResponse();
36
    }
37
38 2
    public function __toString(): string
39
    {
40 2
        return (string)print_r($this->toArray(), true);
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46 10
    public static function init($data): static
47
    {
48 10
        return new static($data);
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54 2
    public function isSuccess(): bool
55
    {
56 2
        if (Arr::has($this->response, 'status') && is_bool(Arr::get($this->response, 'status'))) {
57 2
            return Arr::get($this->response, 'status');
58
        }
59
60 1
        return !(Arr::has($this->response, 'status') && Arr::has($this->response, 'instance'));
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66 2
    public function getResponse(): array
67
    {
68 2
        return $this->response;
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74 2
    public function getErrorMessage(): string
75
    {
76 2
        if ($this->isSuccess()) {
77 1
            return '';
78
        }
79
80 2
        if (Arr::has($this->response, 'errors.0')) {
81 2
            return Arr::get($this->response, 'title', 'Error') . ': ' . Arr::get($this->response, 'errors.0.message');
82
        }
83
84 1
        return Arr::get($this->response, 'title', 'Error') . ': ' . Arr::get($this->response, 'detail', 'Request error');
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90 2
    public function get(?string $key = null, mixed $default = null): mixed
91
    {
92 2
        if (!is_null($key)) {
93 2
            return Arr::get($this->getResponse(), $key, $default);
94
        }
95
96 2
        return $this->getResponse();
97
    }
98
}
99