public function cleanup(UserProfile $prop) { if ($prop->getProperty()->getFieldType() === ProfileProperty::TYPE_FILE) { /*$fileName = str_replace($this->getBaseFileUrl() . '/', '', $prop->getPropertyValue()); if ( is_file($this->getBaseFilePath() . '/' . $fileName) ) { echo 'maybe removing ' . $this->getBaseFilePath() . '/' . $fileName; @unlink($this->getBaseFilePath() . '/' . $fileName); }*/ $fileKey = AbstractUploader::extractKey($prop->getPropertyValue(), $this->awsBucket); if ($fileKey && $this->filesystem->has($fileKey)) { $this->filesystem->delete($fileKey); } } }
public function setProperty(User $user, $key, $value, $visibility = null, $forceClear = false) { // hate this, but alternatives will take a long time to implement and restructure. if (!$this->propertyHandler) { throw new \RuntimeException('Call setPropertyHandler before setProperty'); } $propReference = $this->getPropertyReference($key); if (!$propReference) { return false; } $property = $this->getProfileProperty($user, $key); if ($property === null && !empty($value)) { $newProp = new UserProfile(); $newProp->setUser($user); $newProp->setProperty($propReference); $newProp->setPropertyValue($value); $newProp->setVisibility($visibility ?: $propReference->getDefaultVisibility()); $this->getEntityManager()->persist($newProp); } else { if ($property) { // for file fields, do not overwrite if the given value is blank. if ($propReference->getFieldType() === ProfileProperty::TYPE_FILE && empty($value) && !$forceClear) { // Set property visibility when bailing early // Maybe fix this for less code duplication later if ($visibility !== null) { $property->setVisibility($visibility); } $property->setLastModified(new \DateTime('now')); $this->getEntityManager()->flush(); return true; } // remove old property and clean up (remove files, etc.) $this->propertyHandler->cleanup($property); $property->setPropertyValue($value); if ($visibility !== null) { $property->setVisibility($visibility); } $property->setLastModified(new \DateTime('now')); } } $this->getEntityManager()->flush(); return true; }