1 | <?php |
||
26 | class ContentViewBuilder implements ViewBuilder |
||
27 | { |
||
28 | /** @var \eZ\Publish\API\Repository\Repository */ |
||
29 | private $repository; |
||
30 | |||
31 | /** @var AuthorizationCheckerInterface */ |
||
32 | private $authorizationChecker; |
||
33 | |||
34 | /** @var \eZ\Publish\Core\MVC\Symfony\View\Configurator */ |
||
35 | private $viewConfigurator; |
||
36 | |||
37 | /** @var \eZ\Publish\Core\MVC\Symfony\View\ParametersInjector */ |
||
38 | private $viewParametersInjector; |
||
39 | |||
40 | /** |
||
41 | * Default templates, indexed per viewType (full, line, ...). |
||
42 | * @var array |
||
43 | */ |
||
44 | private $defaultTemplates; |
||
45 | |||
46 | public function __construct( |
||
57 | |||
58 | public function matches($argument) |
||
62 | |||
63 | /** |
||
64 | * @param array $parameters |
||
65 | * |
||
66 | * @return \eZ\Publish\Core\MVC\Symfony\View\ContentView|\eZ\Publish\Core\MVC\Symfony\View\View |
||
67 | * If both contentId and locationId parameters are missing |
||
68 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException |
||
69 | * If both contentId and locationId parameters are missing |
||
70 | * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
71 | */ |
||
72 | public function buildView(array $parameters) |
||
73 | { |
||
74 | $view = new ContentView(null, [], $parameters['viewType']); |
||
75 | $view->setIsEmbed($this->isEmbed($parameters)); |
||
76 | |||
77 | if ($view->isEmbed() && $parameters['viewType'] === null) { |
||
78 | $view->setViewType(EmbedView::DEFAULT_VIEW_TYPE); |
||
79 | } |
||
80 | |||
81 | if (isset($parameters['locationId'])) { |
||
82 | $location = $this->loadLocation($parameters['locationId']); |
||
83 | } elseif (isset($parameters['location'])) { |
||
84 | $location = $parameters['location']; |
||
85 | } else { |
||
86 | $location = null; |
||
87 | } |
||
88 | |||
89 | if (isset($parameters['content'])) { |
||
90 | $content = $parameters['content']; |
||
91 | } else { |
||
92 | if (isset($parameters['contentId'])) { |
||
93 | $contentId = $parameters['contentId']; |
||
94 | } elseif (isset($location)) { |
||
95 | $contentId = $location->contentId; |
||
96 | } else { |
||
97 | throw new InvalidArgumentException('Content', 'No content could be loaded from parameters'); |
||
98 | } |
||
99 | |||
100 | $content = $view->isEmbed() ? $this->loadContent($contentId) : $this->loadEmbeddedContent($contentId, $location); |
||
101 | } |
||
102 | |||
103 | $view->setContent($content); |
||
104 | if (isset($location)) { |
||
105 | if ($location->contentId !== $content->id) { |
||
106 | throw new InvalidArgumentException('Location', 'Provided location does not belong to selected content'); |
||
107 | } |
||
108 | |||
109 | $view->setLocation($location); |
||
110 | } |
||
111 | |||
112 | $this->viewParametersInjector->injectViewParameters($view, $parameters); |
||
113 | $this->viewConfigurator->configure($view); |
||
114 | |||
115 | // deprecated controller actions are replaced with their new equivalent, viewAction and embedAction |
||
116 | if (!$view->getControllerReference() instanceof ControllerReference) { |
||
117 | if (in_array($parameters['_controller'], ['ez_content:viewLocation', 'ez_content:viewContent'])) { |
||
118 | $view->setControllerReference(new ControllerReference('ez_content:viewAction')); |
||
119 | } elseif (in_array($parameters['_controller'], ['ez_content:embedLocation', 'ez_content:embedContent'])) { |
||
120 | $view->setControllerReference(new ControllerReference('ez_content:embedAction')); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | return $view; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Loads Content with id $contentId. |
||
129 | * |
||
130 | * @param mixed $contentId |
||
131 | * |
||
132 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
133 | * |
||
134 | * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
135 | */ |
||
136 | private function loadContent($contentId) |
||
140 | |||
141 | /** |
||
142 | * Loads the embedded content with id $contentId. |
||
143 | * Will load the content with sudo(), and check if the user can view_embed this content, for the given location |
||
144 | * if provided. |
||
145 | * |
||
146 | * @param mixed $contentId |
||
147 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
148 | * |
||
149 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
150 | * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
151 | */ |
||
152 | private function loadEmbeddedContent($contentId, Location $location = null) |
||
179 | |||
180 | /** |
||
181 | * Loads a visible Location. |
||
182 | * @todo Do we need to handle permissions here ? |
||
183 | * |
||
184 | * @param $locationId |
||
185 | * |
||
186 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
187 | */ |
||
188 | private function loadLocation($locationId) |
||
201 | |||
202 | /** |
||
203 | * Checks if a user can read a content, or view it as an embed. |
||
204 | * |
||
205 | * @param Content $content |
||
206 | * @param $location |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | private function canRead(Content $content, Location $location = null) |
||
224 | |||
225 | /** |
||
226 | * Checks if the view is an embed one. |
||
227 | * Uses either the controller action (embedAction), or the viewType (embed/embed-inline). |
||
228 | * |
||
229 | * @param array $parameters The ViewBuilder parameters array. |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | private function isEmbed($parameters) |
||
244 | } |
||
245 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: