Conditions | 8 |
Paths | 14 |
Total Lines | 52 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
72 | private function handlePUTRequest(ClassMetadataInfo $classMetadata, Publishable $configuration, object $data, Request $request): void |
||
73 | { |
||
74 | $changeSet = $this->getEntityManager($data)->getUnitOfWork()->getEntityChangeSet($data); |
||
75 | |||
76 | // It's not possible to change the publishedResource property |
||
77 | if (isset($changeSet[$configuration->associationName])) { |
||
78 | $classMetadata->setFieldValue($data, $configuration->associationName, $changeSet[$configuration->associationName][0]); |
||
79 | } |
||
80 | |||
81 | // User doesn't have draft access: cannot change the publication date |
||
82 | if (!$this->publishableHelper->isGranted()) { |
||
83 | if (isset($changeSet[$configuration->fieldName])) { |
||
84 | $classMetadata->setFieldValue($data, $configuration->fieldName, $changeSet[$configuration->fieldName][0]); |
||
85 | } |
||
86 | |||
87 | // Nothing to do here anymore for user without draft access |
||
88 | return; |
||
89 | } |
||
90 | |||
91 | // User requested for original object |
||
92 | if (true === $request->query->getBoolean('published', false)) { |
||
93 | // User cannot change the publication date of the original resource |
||
94 | if ($changeSet[$configuration->fieldName]) { |
||
95 | throw new BadRequestHttpException('You cannot change the publication date of a published resource.'); |
||
96 | } |
||
97 | |||
98 | // User wants to update the original object: nothing to do here anymore |
||
99 | return; |
||
100 | } |
||
101 | |||
102 | // Resource is a draft of another resource: nothing to do here anymore |
||
103 | if (null !== $classMetadata->getFieldValue($data, $configuration->associationName)) { |
||
104 | return; |
||
105 | } |
||
106 | |||
107 | // Any field has been modified: create or update draft |
||
108 | $draft = $this->getEntityManager($data)->getRepository($this->getObjectClass($data))->findOneBy([ |
||
109 | $configuration->associationName => $data, |
||
110 | ]); |
||
111 | if (!$draft) { |
||
112 | $draft = clone $data; |
||
113 | // Reset draft identifier(s) |
||
114 | $classMetadata->setIdentifierValues($draft, array_combine($classMetadata->getIdentifierFieldNames(), array_fill(0, \count($classMetadata->getIdentifierFieldNames()), null))); |
||
115 | |||
116 | $this->getEntityManager($draft)->persist($draft); |
||
117 | } |
||
118 | |||
119 | // Replace data by its draft |
||
120 | $request->attributes->set('data', $draft); |
||
121 | |||
122 | // Rollback modifications on original resource |
||
123 | $this->getEntityManager($data)->refresh($data); |
||
124 | } |
||
126 |