• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

h4kuna / exchange-nette / 13364378811

17 Feb 2025 06:38AM UTC coverage: 81.188% (-0.5%) from 81.683%
13364378811

push

github

h4kuna
feat(composer): update phpstan

3 of 5 new or added lines in 2 files covered. (60.0%)

164 of 202 relevant lines covered (81.19%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

13.16
/src/ExchangeManager.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\Exchange;
4

5
use Nette;
6
use Nette\Http;
7

8
class ExchangeManager
9
{
10
        use Nette\SmartObject;
11

12
        private const EMPTY_CODE = '';
13

14
        /** @var array<callable> */
15
        public array $onChangeCurrency;
16

17
        protected ?Http\SessionSection $session = null;
18

19
        protected string $parameter = 'currency';
20

21

22
        public function __construct(
1✔
23
                private Exchange $exchange,
24
                private Http\Request $request,
25
                private Http\Response $response,
26
        )
27
        {
28
        }
1✔
29

30

31
        public function setParameter(string $parameter): void
1✔
32
        {
33
                $this->parameter = $parameter;
1✔
34
        }
1✔
35

36

37
        /**
38
         * @param mixed $presenter
39
         */
40
        public function init($presenter): void
41
        {
42
                $code = $this->setCurrency($this->getQuery());
×
43
                if ($code === self::EMPTY_CODE) {
×
44
                        if ($this->initCookie() === self::EMPTY_CODE) {
×
45
                                $this->initSession();
×
46
                        }
47
                } else {
48
                        $this->onChangeCurrency($presenter, $code);
×
49
                }
50
        }
51

52

53
        public function setCurrency(string $code): string
54
        {
55
                if ($code === self::EMPTY_CODE) {
×
56
                        return self::EMPTY_CODE;
×
57
                }
58
                $code = strtoupper($code);
×
59

60
                if ($this->exchange->offsetExists($code) === false) {
×
61
                        return self::EMPTY_CODE;
×
62
                }
63

64
                $this->saveCookie($code);
×
65
                $this->saveSession($code);
×
66

67
                return $code;
×
68
        }
69

70

71
        protected function saveCookie(string $code): void
72
        {
73
                $this->response->setCookie($this->parameter, $code, '+6 month');
×
74
        }
75

76

77
        protected function saveSession(string $code): void
78
        {
79
                if ($this->session === null) {
×
80
                        return;
×
81
                }
82
                $this->session->{$this->parameter} = $code;
×
83
                $this->session->setExpiration('+1 days');
×
84
        }
85

86

87
        protected function getQuery(): string
88
        {
89
                $value = $this->request->getQuery($this->parameter);
×
90
                assert($value === null || is_string($value));
91

92
                return (string) $value;
×
93
        }
94

95

96
        private function initCookie(): string
97
        {
98
                $code = $this->setCurrency($this->getCookie());
×
99
                if ($code === self::EMPTY_CODE) {
×
100
                        $this->deleteCookie();
×
101
                }
102

103
                return $code;
×
104
        }
105

106

107
        protected function getCookie(): string
108
        {
109
                $value = $this->request->getCookie($this->parameter);
×
110
                assert($value === null || is_string($value));
111

112
                return (string) $value;
×
113
        }
114

115

116
        protected function deleteCookie(): void
117
        {
118
                $this->response->deleteCookie($this->parameter);
×
119
        }
120

121

122
        private function initSession(): void
123
        {
124
                if ($this->session === null) {
×
125
                        return;
×
126
                }
127
                $this->setCurrency($this->getSession());
×
128
        }
129

130

131
        protected function getSession(): string
132
        {
NEW
133
                $value = $this->session->{$this->parameter};
×
134
                assert($value === null || is_string($value));
135

NEW
136
                return (string) $value;
×
137
        }
138

139

140
        public function setSession(Http\SessionSection $session): void
141
        {
142
                $this->session = $session;
×
143
        }
144

145
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc