Passed
Pull Request — master (#166)
by
unknown
21:05
created

AjaxBackofficeController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 119
rs 10
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
B addWorkspaceFilterAction() 0 19 9
A addWorkspaceSortAction() 0 4 1
A removeBookmarkAction() 0 10 2
A setWorkspaceItemsPerPageAction() 0 4 1
A toggleWorkspaceExcludeDiscardedAction() 0 4 1
A addBookmarkAction() 0 13 2
A toggleWorkspaceBookmarksOnlyAction() 0 4 1
1
<?php
2
namespace EWW\Dpf\Controller;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19
/**
20
 * AjaxBackofficeController
21
 */
22
class AjaxBackofficeController extends \EWW\Dpf\Controller\AbstractController
23
{
24
    /**
25
     * bookmarkRepository
26
     *
27
     * @var \EWW\Dpf\Domain\Repository\BookmarkRepository
28
     * @inject
29
     */
30
    protected $bookmarkRepository = null;
31
32
33
    /**
34
     * Adds a the given document identifier to the bookmark list of the current fe user.
35
     *
36
     * @param string $identifier
37
     * @return bool
38
     */
39
    public function addBookmarkAction($identifier)
40
    {
41
        /** @var \EWW\Dpf\Domain\Model\Bookmark $bookmark */
42
        $bookmark = $this->bookmarkRepository->findBookmark($this->security->getUser()->getUid(), $identifier);
43
        if (!$bookmark) {
0 ignored issues
show
introduced by
$bookmark is of type EWW\Dpf\Domain\Model\Bookmark, thus it always evaluated to true.
Loading history...
44
            $bookmark = $this->objectManager->get(\EWW\Dpf\Domain\Model\Bookmark::class);
45
            $bookmark->setDocumentIdentifier($identifier);
46
            $bookmark->setFeUserUid($this->security->getUser()->getUid());
47
            $this->bookmarkRepository->add($bookmark);
48
            return true;
49
        }
50
51
        return false;
52
    }
53
54
    /**
55
     * Removes the given document from the bookmark list of the current fe user.
56
     *
57
     * @param string $identifier
58
     * @return bool
59
     */
60
    public function removeBookmarkAction($identifier)
61
    {
62
        /** @var \EWW\Dpf\Domain\Model\Bookmark $bookmark */
63
        $bookmark = $this->bookmarkRepository->findBookmark($this->security->getUser()->getUid(), $identifier);
64
        if ($bookmark) {
0 ignored issues
show
introduced by
$bookmark is of type EWW\Dpf\Domain\Model\Bookmark, thus it always evaluated to true.
Loading history...
65
            $this->bookmarkRepository->remove($bookmark);
66
            return true;
67
        }
68
69
        return false;
70
    }
71
72
    /**
73
     * Adds a workspace filter to the session.
74
     *
75
     * @param string $name
76
     * @param array $values
77
     */
78
    public function addWorkspaceFilterAction($name, $values = [])
79
    {
80
        if ($name && $values && is_array($values)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $values of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
81
            $workspaceFilters = $this->getSessionData('workspaceFilters');
82
            if ($workspaceFilters && is_array($workspaceFilters)) {
83
                $workspaceFilters[$name] = $values;
84
                $this->setSessionData('workspaceFilters', $workspaceFilters);
0 ignored issues
show
Bug introduced by
$workspaceFilters of type array is incompatible with the type string expected by parameter $data of EWW\Dpf\Controller\Abstr...oller::setSessionData(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
                $this->setSessionData('workspaceFilters', /** @scrutinizer ignore-type */ $workspaceFilters);
Loading history...
85
            } else {
86
                $this->setSessionData('workspaceFilters', [$name => $values]);
0 ignored issues
show
Bug introduced by
array($name => $values) of type array<string,array> is incompatible with the type string expected by parameter $data of EWW\Dpf\Controller\Abstr...oller::setSessionData(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
                $this->setSessionData('workspaceFilters', /** @scrutinizer ignore-type */ [$name => $values]);
Loading history...
87
            }
88
        } else {
89
            $workspaceFilters = $this->getSessionData('workspaceFilters');
90
            if ($name && is_array($workspaceFilters) && array_key_exists($name, $workspaceFilters)) {
91
                unset($workspaceFilters[$name]);
92
                $this->setSessionData('workspaceFilters', $workspaceFilters);
0 ignored issues
show
Bug introduced by
$workspaceFilters of type array is incompatible with the type string expected by parameter $data of EWW\Dpf\Controller\Abstr...oller::setSessionData(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
                $this->setSessionData('workspaceFilters', /** @scrutinizer ignore-type */ $workspaceFilters);
Loading history...
93
            }
94
        }
95
96
        return;
97
98
    }
99
100
    /**
101
     * Adds a workspace sort to the session.
102
     *
103
     * @param string $field
104
     * @param string $order
105
     */
106
    public function addWorkspaceSortAction($field, $order)
107
    {
108
        $this->session->setWorkspaceSort($field, $order);
109
        return;
110
    }
111
112
    /**
113
     * Toggles the filter to exclude discarded documents.
114
     *
115
     */
116
    public function toggleWorkspaceExcludeDiscardedAction()
117
    {
118
        $this->session->toggleWorkspaceExcludeDiscardedFilter();
119
        return;
120
    }
121
122
    /**
123
     * Toggles the filter to hide bookmarked documents.
124
     *
125
     */
126
    public function toggleWorkspaceBookmarksOnlyAction()
127
    {
128
        $this->session->toggleWorkspaceBookmarksOnlyFilter();
129
        return;
130
    }
131
132
    /**
133
     * Sets the items per page for the workspace list.
134
     *
135
     * @param int $itemsPerPage
136
     */
137
    public function setWorkspaceItemsPerPageAction($itemsPerPage)
138
    {
139
        $this->session->setWorkspaceItemsPerPage($itemsPerPage);
140
        return;
141
    }
142
143
144
}
145