LockController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 20
c 0
b 0
f 0
dl 0
loc 53
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 5 1
A lock() 0 21 2
A add() 0 5 1
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2021 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
namespace App\Controller;
14
15
use BEdita\SDK\BEditaClientException;
16
use Cake\Http\Response;
17
use Psr\Log\LogLevel;
18
19
/**
20
 * Lock Controller
21
 */
22
class LockController extends AppController
23
{
24
    /**
25
     * Add lock
26
     *
27
     * @return \Cake\Http\Response|null
28
     */
29
    public function add(): ?Response
30
    {
31
        $this->lock(true);
32
33
        return $this->redirect($this->referer());
34
    }
35
36
    /**
37
     * Remove lock
38
     *
39
     * @return \Cake\Http\Response|null
40
     */
41
    public function remove(): ?Response
42
    {
43
        $this->lock(false);
44
45
        return $this->redirect($this->referer());
46
    }
47
48
    /**
49
     * Perform lock/unlock on an object.
50
     *
51
     * @param bool $locked The value, true or false
52
     * @return bool
53
     */
54
    protected function lock(bool $locked): bool
55
    {
56
        $type = $this->getRequest()->getParam('object_type');
57
        $id = $this->getRequest()->getParam('id');
58
        $meta = compact('locked');
59
        $data = compact('id', 'type', 'meta');
60
        $payload = json_encode(compact('data'));
61
        try {
62
            $this->apiClient->patch(
63
                sprintf('/%s/%s', $type, $id),
64
                $payload,
65
                ['Content-Type' => 'application/json']
66
            );
67
        } catch (BEditaClientException $ex) {
68
            $this->log($ex->getMessage(), LogLevel::ERROR);
69
            $this->Flash->error(__('Error: %s', $ex->getMessage()));
70
71
            return false;
72
        }
73
74
        return true;
75
    }
76
}
77