Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.50% covered (warning)
82.50%
33 / 40
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
BlockedIpsController
82.50% covered (warning)
82.50%
33 / 40
40.00% covered (danger)
40.00%
2 / 5
10.54
0.00% covered (danger)
0.00%
0 / 1
 index
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 view
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 add
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
3.24
 edit
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
3.24
 delete
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
2.01
1<?php
2declare(strict_types=1);
3
4namespace App\Controller\Admin;
5
6use App\Controller\Admin\Trait\SearchableTrait;
7use App\Controller\AppController;
8use Cake\Cache\Cache;
9use Cake\Http\Response;
10
11/**
12 * BlockedIps Controller
13 *
14 * @property \App\Model\Table\BlockedIpsTable $BlockedIps
15 */
16class BlockedIpsController extends AppController
17{
18    use SearchableTrait;
19
20    /**
21     * Index method
22     *
23     * @return \Cake\Http\Response|null|void Renders view
24     */
25    public function index(): ?Response
26    {
27        $query = $this->BlockedIps->find()
28            ->select([
29                'BlockedIps.id',
30                'BlockedIps.ip_address',
31                'BlockedIps.reason',
32                'BlockedIps.blocked_at',
33                'BlockedIps.expires_at',
34                'BlockedIps.created',
35                'BlockedIps.modified',
36            ]);
37
38        return $this->handleSearch($query, 'blockedIps', ['ip_address', 'reason']);
39    }
40
41    /**
42     * View method
43     *
44     * @param string|null $id Blocked Ip id.
45     * @return \Cake\Http\Response|null|void Renders view
46     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
47     */
48    public function view(?string $id = null): void
49    {
50        $blockedIp = $this->BlockedIps->get($id, contain: []);
51        $this->set(compact('blockedIp'));
52    }
53
54    /**
55     * Add method
56     *
57     * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
58     */
59    public function add(): ?Response
60    {
61        $blockedIp = $this->BlockedIps->newEmptyEntity();
62        if ($this->request->is('post')) {
63            $blockedIp = $this->BlockedIps->patchEntity($blockedIp, $this->request->getData());
64            if ($this->BlockedIps->save($blockedIp)) {
65                Cache::clear('ip_blocker');
66                $this->Flash->success(__('The blocked ip has been saved.'));
67
68                return $this->redirect(['action' => 'index']);
69            }
70            $this->Flash->error(__('The blocked ip could not be saved. Please, try again.'));
71        }
72        $this->set(compact('blockedIp'));
73
74        return null;
75    }
76
77    /**
78     * Edit method
79     *
80     * @param string|null $id Blocked Ip id.
81     * @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
82     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
83     */
84    public function edit(?string $id = null): ?Response
85    {
86        $blockedIp = $this->BlockedIps->get($id, contain: []);
87        if ($this->request->is(['patch', 'post', 'put'])) {
88            $blockedIp = $this->BlockedIps->patchEntity($blockedIp, $this->request->getData());
89            if ($this->BlockedIps->save($blockedIp)) {
90                Cache::clear('ip_blocker');
91                $this->Flash->success(__('The blocked ip has been saved.'));
92
93                return $this->redirect(['action' => 'index']);
94            }
95            $this->Flash->error(__('The blocked ip could not be saved. Please, try again.'));
96        }
97        $this->set(compact('blockedIp'));
98
99        return null;
100    }
101
102    /**
103     * Delete method
104     *
105     * @param string|null $id Blocked Ip id.
106     * @return \Cake\Http\Response|null Redirects to index.
107     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
108     */
109    public function delete(?string $id = null): ?Response
110    {
111        $this->request->allowMethod(['post', 'delete']);
112        $blockedIp = $this->BlockedIps->get($id);
113        if ($this->BlockedIps->delete($blockedIp)) {
114            $this->Flash->success(__('The blocked ip has been deleted.'));
115            Cache::clear('ip_blocker');
116        } else {
117            $this->Flash->error(__('The blocked ip could not be deleted. Please, try again.'));
118        }
119
120        return $this->redirect($this->referer());
121    }
122}