SendyPHP::subscribe()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests\Mocks;
6
7
use SendyPHP\SendyPHP as Base;
8
9
final class SendyPHP extends Base
10
{
11
    /** @var array<string> */
12
    private array $lists = [
13
        'example_list',
14
        'example_list2',
15
    ];
16
17
    /**
18
     * @var array<string, array<int, string>>
19
     */
20
    private array $subscribers = [
21
        'example_list' => [
22
            '[email protected]',
23
            '[email protected]',
24
        ],
25
        'example_list2' => [],
26
    ];
27
28
    /**
29
     * @param string|mixed $list
30
     *
31
     * @return array<string, bool|string>
32
     */
33
    public function subcount($list = ''): array
34
    {
35
        $list = '' === $list ? $this->list_id : $list;
36
37
        if (!in_array($list, $this->lists, true)) {
38
            return $this->response(false, 'Error.');
39
        }
40
41
        return $this->response(true, (string) count($this->subscribers[$list]));
42
    }
43
44
    /**
45
     * @param string|mixed $email
46
     *
47
     * @return array<string, bool|string>
48
     */
49
    public function substatus($email): array
50
    {
51
        if (!in_array($this->list_id, $this->lists, true) ||
52
            !in_array($email, $this->subscribers[$this->list_id], true)) {
53
            return $this->response(false, 'Error.');
54
        }
55
56
        return $this->response(true, 'Subscribed');
57
    }
58
59
    /**
60
     * @param array<string, string>|array<mixed> $values
61
     *
62
     * @return array<string, bool|string>
63
     */
64
    public function subscribe(array $values): array
65
    {
66
        if (!in_array($this->list_id, $this->lists, true)) {
67
            return $this->response(false, 'Error.');
68
        }
69
70
        if (in_array($values['email'], $this->subscribers[$this->list_id], true)) {
71
            return $this->response(true, 'Already subscribed');
72
        }
73
74
        return $this->response(true, 'Subscribed');
75
    }
76
77
    /**
78
     * @param string|mixed $email
79
     *
80
     * @return array<string, bool|string>
81
     */
82
    public function unsubscribe($email): array
83
    {
84
        if (!in_array($this->list_id, $this->lists, true) ||
85
            !in_array($email, $this->subscribers[$this->list_id], true)) {
86
            return $this->response(false, 'Error.');
87
        }
88
89
        return $this->response(true, 'Unsubscribed');
90
    }
91
92
    /**
93
     * @return array<string, bool|string>
94
     */
95
    private function response(bool $status, string $message = ''): array
96
    {
97
        return [
98
            'status' => $status,
99
            'message' => $message,
100
        ];
101
    }
102
}
103