Conditions | 8 |
Paths | 8 |
Total Lines | 52 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
40 | public function onKernelResponse(ResponseEvent $event): void |
||
41 | { |
||
42 | $request = $event->getRequest(); |
||
43 | // Prevent issues with NelmioCorsBundle |
||
44 | if ($this->isPreflightRequest($request)) { |
||
45 | return; |
||
46 | } |
||
47 | |||
48 | $subscribeIris = []; |
||
49 | $response = $event->getResponse(); |
||
50 | |||
51 | foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
||
52 | $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass); |
||
53 | |||
54 | try { |
||
55 | $operation = $resourceMetadataCollection->getOperation(forceCollection: false, httpOperation: true); |
||
56 | } catch (OperationNotFoundException $e) { |
||
57 | continue; |
||
58 | } |
||
59 | |||
60 | if (!$operation instanceof HttpOperation) { |
||
61 | continue; |
||
62 | } |
||
63 | |||
64 | $mercure = $operation->getMercure(); |
||
65 | |||
66 | if (!$mercure) { |
||
67 | continue; |
||
68 | } |
||
69 | |||
70 | $refl = new \ReflectionClass($operation->getClass()); |
||
71 | $isPublishable = \count($refl->getAttributes(Publishable::class)); |
||
72 | |||
73 | // TODO: the str_replace thing should be fixed inside API Platform (will be available in next patch) |
||
74 | $uriTemplate = $this->buildAbsoluteUriTemplate() . $operation->getRoutePrefix() . str_replace('.{_format}', '{._format}', $operation->getUriTemplate()); |
||
75 | |||
76 | if (!$isPublishable) { |
||
77 | $subscribeIris[] = $uriTemplate; |
||
78 | continue; |
||
79 | } |
||
80 | |||
81 | // Note that `?draft=1` is also hard coded into the PublishableIriConverter, probably make this configurable somewhere |
||
82 | if ($this->publishableStatusChecker->isGranted($operation->getClass())) { |
||
83 | $subscribeIris[] = $uriTemplate . '?draft=1'; |
||
84 | $subscribeIris[] = $uriTemplate; |
||
85 | continue; |
||
86 | } |
||
87 | |||
88 | $subscribeIris[] = $uriTemplate; |
||
89 | } |
||
90 | |||
91 | $response->headers->setCookie(Cookie::create('mercureAuthorization', $this->tokenFactory->create($subscribeIris, []))); |
||
92 | } |
||
113 |