Completed
Push — master ( 4896ac...e378d5 )
by Łukasz
24:08
created

Bookmark::deleteBookmark()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 3
nop 2
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\REST\Server\Controller;
10
11
use eZ\Publish\API\Repository\BookmarkService;
12
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
13
use eZ\Publish\API\Repository\LocationService;
14
use eZ\Publish\Core\REST\Common\Exceptions;
15
use eZ\Publish\Core\REST\Common\Value as RestValue;
16
use eZ\Publish\Core\REST\Server\Values;
17
use eZ\Publish\Core\REST\Server\Controller as RestController;
18
use Symfony\Component\HttpFoundation\Request;
19
20
class Bookmark extends RestController
21
{
22
    /**
23
     * @var \eZ\Publish\API\Repository\BookmarkService
24
     */
25
    protected $bookmarkService;
26
27
    /**
28
     * @var \eZ\Publish\API\Repository\LocationService
29
     */
30
    protected $locationService;
31
32
    /**
33
     * Bookmark constructor.
34
     *
35
     * @param \eZ\Publish\API\Repository\BookmarkService $bookmarkService
36
     * @param \eZ\Publish\API\Repository\LocationService $locationService
37
     */
38
    public function __construct(BookmarkService $bookmarkService, LocationService $locationService)
39
    {
40
        $this->bookmarkService = $bookmarkService;
41
        $this->locationService = $locationService;
42
    }
43
44
    /**
45
     * Add given location to bookmarks.
46
     *
47
     * @param \Symfony\Component\HttpFoundation\Request $request
48
     * @param int $locationId
49
     *
50
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
51
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
52
     *
53
     * @return \eZ\Publish\Core\REST\Common\Value
54
     */
55
    public function createBookmark(Request $request, int $locationId): RestValue
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
    {
57
        $location = $this->locationService->loadLocation($locationId);
58
59
        try {
60
            $this->bookmarkService->createBookmark($location);
61
62
            return new Values\ResourceCreated(
63
                $this->router->generate(
64
                    'ezpublish_rest_isBookmarked',
65
                    [
66
                        'locationId' => $locationId,
67
                    ]
68
                )
69
            );
70
        } catch (InvalidArgumentException $e) {
71
            return new Values\Conflict();
72
        }
73
    }
74
75
    /**
76
     * Deletes a given bookmark.
77
     *
78
     * @param \Symfony\Component\HttpFoundation\Request $request
79
     * @param int $locationId
80
     *
81
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
82
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
83
     *
84
     * @return \eZ\Publish\Core\REST\Common\Value
85
     */
86 View Code Duplication
    public function deleteBookmark(Request $request, int $locationId): RestValue
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88
        $location = $this->locationService->loadLocation($locationId);
89
90
        try {
91
            $this->bookmarkService->deleteBookmark($location);
92
93
            return new Values\NoContent();
94
        } catch (InvalidArgumentException $e) {
95
            throw new Exceptions\NotFoundException("Location {$locationId} is not bookmarked");
96
        }
97
    }
98
99
    /**
100
     * Checks if given location is bookmarked.
101
     *
102
     * @param \Symfony\Component\HttpFoundation\Request $request
103
     * @param int $locationId
104
     *
105
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
106
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
107
     *
108
     * @return \eZ\Publish\Core\REST\Server\Values\OK
109
     */
110 View Code Duplication
    public function isBookmarked(Request $request, int $locationId): Values\OK
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
    {
112
        $location = $this->locationService->loadLocation($locationId);
113
114
        if (!$this->bookmarkService->isBookmarked($location)) {
115
            throw new Exceptions\NotFoundException("Location {$locationId} is not bookmarked");
116
        }
117
118
        return new Values\OK();
119
    }
120
121
    /**
122
     * List bookmarked locations.
123
     *
124
     * @param \Symfony\Component\HttpFoundation\Request $request
125
     *
126
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
127
     *
128
     * @return \eZ\Publish\Core\REST\Common\Value
129
     */
130
    public function loadBookmarks(Request $request): RestValue
131
    {
132
        $offset = $request->query->getInt('offset', 0);
133
        $limit = $request->query->getInt('limit', 25);
134
135
        $restLocations = [];
136
        $bookmarks = $this->bookmarkService->loadBookmarks($offset, $limit);
137
        foreach ($bookmarks as $bookmark) {
138
            $restLocations[] = new Values\RestLocation(
139
                $bookmark,
140
                $this->locationService->getLocationChildCount($bookmark)
141
            );
142
        }
143
144
        return new Values\BookmarkList($bookmarks->totalCount, $restLocations);
145
    }
146
147
    /**
148
     * Extracts and returns an item id from a path, e.g. /1/2/58 => 58.
149
     *
150
     * @param string $path
151
     *
152
     * @return mixed
153
     */
154
    private function extractLocationIdFromPath(string $path)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
155
    {
156
        $pathParts = explode('/', $path);
157
158
        return array_pop($pathParts);
159
    }
160
}
161