Service::addCookie()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 13
rs 10
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xhelp;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       Brian Wahoff <[email protected]>
19
 * @author       XOOPS Development Team
20
 */
21
22
/**
23
 * Service class
24
 *
25
 * Part of the Messaging Subsystem. Provides the base interface for subscribing, unsubcribing from events
26
 *
27
 *
28
 * @author  Brian Wahoff <[email protected]>
29
 */
30
class Service
31
{
32
    public  $_cookies = [];
33
    private $eventService;
34
    private $helper;
0 ignored issues
show
introduced by
The private property $helper is not used, and could be removed.
Loading history...
35
36
    /**
37
     * @param string  $eventName
38
     * @param Service $callback
39
     */
40
    public function attachEvent(string $eventName, Service $callback): void
41
    {
42
        $this->addCookie($eventName, (string)$this->eventService->advise($eventName, $callback));
43
    }
44
45
    public function init(): void
46
    {
47
        $this->eventService = EventService::getInstance();
48
        $this->attachEvents();
49
    }
50
51
    public function attachEvents(): void
52
    {
53
        //Do nothing (must implement this function in subclasses)
54
    }
55
56
    public function detachEvents(): void
57
    {
58
        foreach ($this->_cookies as $event => $cookie) {
59
            if (\is_array($cookie)) {
60
                foreach ($cookie as $ele) {
61
                    $this->eventService->unadvise($event, $ele);
62
                }
63
            } else {
64
                $this->eventService->unadvise($event, $cookie);
65
            }
66
        }
67
        $this->_cookies = [];
68
    }
69
70
    /**
71
     * @param string $eventName
72
     */
73
    public function detachFromEvent(string $eventName): void
74
    {
75
        if (isset($this->_cookies[$eventName])) {
76
            $cookie = $this->_cookies[$eventName];
77
            if (\is_array($cookie)) {
78
                foreach ($cookie as $ele) {
79
                    $this->eventService->unadvise($eventName, $ele);
80
                }
81
            } else {
82
                $this->eventService->unadvise($eventName, $cookie);
83
            }
84
            unset($this->_cookies[$eventName]);
85
        }
86
    }
87
88
    /**
89
     * @param string $eventName
90
     * @param string $cookie
91
     */
92
    private function addCookie(string $eventName, string $cookie): void
93
    {
94
        //Check if the cookie already exist
95
        if (!isset($this->_cookies[$eventName])) {
96
            //Cookie doesn't exist
97
            $this->_cookies[$eventName] = $cookie;
98
        } elseif (\is_array($this->_cookies[$eventName])) {
99
            //Already an array, just add new cookie to array
100
            $this->_cookies[$eventName][] = $cookie;
101
        } else {
102
            //A single value, take value and replace it with an array
103
            $oldCookie                  = $this->_cookies[$eventName];
104
            $this->_cookies[$eventName] = [$oldCookie, $cookie];
105
        }
106
    }
107
}
108