Completed
Push — fallback_language_fields ( 531860 )
by André
37:34
created

URLAlias::createURLAlias()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 50
Code Lines 33

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 50
rs 8.8571
cc 4
eloc 33
nc 4
nop 1
1
<?php
2
3
/**
4
 * File containing the URLAlias controller class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\REST\Server\Controller;
12
13
use eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException;
14
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
15
use eZ\Publish\Core\REST\Common\Message;
16
use eZ\Publish\Core\REST\Server\Values;
17
use eZ\Publish\Core\REST\Server\Controller as RestController;
18
use eZ\Publish\API\Repository\URLAliasService;
19
use eZ\Publish\API\Repository\LocationService;
20
use Symfony\Component\HttpFoundation\Request;
21
22
/**
23
 * URLAlias controller.
24
 */
25
class URLAlias extends RestController
26
{
27
    /**
28
     * URLAlias service.
29
     *
30
     * @var \eZ\Publish\API\Repository\URLAliasService
31
     */
32
    protected $urlAliasService;
33
34
    /**
35
     * Location service.
36
     *
37
     * @var \eZ\Publish\API\Repository\LocationService
38
     */
39
    protected $locationService;
40
41
    /**
42
     * Construct controller.
43
     *
44
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
45
     * @param \eZ\Publish\API\Repository\LocationService $locationService
46
     */
47
    public function __construct(URLAliasService $urlAliasService, LocationService $locationService)
48
    {
49
        $this->urlAliasService = $urlAliasService;
50
        $this->locationService = $locationService;
51
    }
52
53
    /**
54
     * Returns the URL alias with the given ID.
55
     *
56
     * @param $urlAliasId
57
     *
58
     * @return \eZ\Publish\API\Repository\Values\Content\URLAlias
59
     */
60
    public function loadURLAlias($urlAliasId)
61
    {
62
        return $this->urlAliasService->load($urlAliasId);
63
    }
64
65
    /**
66
     * Returns the list of global URL aliases.
67
     *
68
     * @return \eZ\Publish\Core\REST\Server\Values\URLAliasRefList
69
     */
70
    public function listGlobalURLAliases()
71
    {
72
        return new Values\URLAliasRefList(
73
            $this->urlAliasService->listGlobalAliases(),
74
            $this->router->generate('ezpublish_rest_listGlobalURLAliases')
75
        );
76
    }
77
78
    /**
79
     * Returns the list of URL aliases for a location.
80
     *
81
     * @param $locationPath
82
     *
83
     * @return \eZ\Publish\Core\REST\Server\Values\URLAliasRefList
84
     */
85
    public function listLocationURLAliases($locationPath, Request $request)
86
    {
87
        $locationPathParts = explode('/', $locationPath);
88
89
        $location = $this->locationService->loadLocation(
90
            array_pop($locationPathParts)
91
        );
92
93
        $custom = $request->query->has('custom') && $request->query->get('custom') === 'false' ? false : true;
94
95
        return new Values\CachedValue(
96
            new Values\URLAliasRefList(
97
                $this->urlAliasService->listLocationAliases($location, $custom),
98
                $request->getPathInfo()
99
            ),
100
            array('locationId' => $location->id)
101
        );
102
    }
103
104
    /**
105
     * Creates a new URL alias.
106
     *
107
     * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException
108
     *
109
     * @return \eZ\Publish\Core\REST\Server\Values\CreatedURLAlias
110
     */
111
    public function createURLAlias(Request $request)
112
    {
113
        $urlAliasCreate = $this->inputDispatcher->parse(
114
            new Message(
115
                array('Content-Type' => $request->headers->get('Content-Type')),
116
                $request->getContent()
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, eZ\Publish\Core\REST\Common\Message::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
117
            )
118
        );
119
120
        if ($urlAliasCreate['_type'] === 'LOCATION') {
121
            $locationPathParts = explode(
122
                '/',
123
                $this->requestParser->parseHref($urlAliasCreate['location']['_href'], 'locationPath')
124
            );
125
126
            $location = $this->locationService->loadLocation(
127
                array_pop($locationPathParts)
128
            );
129
130
            try {
131
                $createdURLAlias = $this->urlAliasService->createUrlAlias(
132
                    $location,
133
                    $urlAliasCreate['path'],
134
                    $urlAliasCreate['languageCode'],
135
                    $urlAliasCreate['forward'],
136
                    $urlAliasCreate['alwaysAvailable']
137
                );
138
            } catch (InvalidArgumentException $e) {
139
                throw new ForbiddenException($e->getMessage());
140
            }
141
        } else {
142
            try {
143
                $createdURLAlias = $this->urlAliasService->createGlobalUrlAlias(
144
                    $urlAliasCreate['resource'],
145
                    $urlAliasCreate['path'],
146
                    $urlAliasCreate['languageCode'],
147
                    $urlAliasCreate['forward'],
148
                    $urlAliasCreate['alwaysAvailable']
149
                );
150
            } catch (InvalidArgumentException $e) {
151
                throw new ForbiddenException($e->getMessage());
152
            }
153
        }
154
155
        return new Values\CreatedURLAlias(
156
            array(
157
                'urlAlias' => $createdURLAlias,
158
            )
159
        );
160
    }
161
162
    /**
163
     * The given URL alias is deleted.
164
     *
165
     * @param $urlAliasId
166
     *
167
     * @return \eZ\Publish\Core\REST\Server\Values\NoContent
168
     */
169
    public function deleteURLAlias($urlAliasId)
170
    {
171
        $this->urlAliasService->removeAliases(
172
            array(
173
                $this->urlAliasService->load($urlAliasId),
174
            )
175
        );
176
177
        return new Values\NoContent();
178
    }
179
}
180