Sulu\Bundle\MediaBundle\Media\Manager\MediaManager::modifyMedia PHP Method

modifyMedia() private method

Modifies an existing media.
private modifyMedia ( Symfony\Component\HttpFoundation\File\UploadedFile $uploadedFile, $data, Sulu\Component\Security\Authentication\UserInterface $user ) : Media
$uploadedFile Symfony\Component\HttpFoundation\File\UploadedFile
$data
$user Sulu\Component\Security\Authentication\UserInterface
return Sulu\Bundle\MediaBundle\Api\Media
    private function modifyMedia($uploadedFile, $data, $user)
    {
        $mediaEntity = $this->getEntityById($data['id']);
        $mediaEntity->setChanger($user);
        $mediaEntity->setChanged(new \DateTime());
        $files = $mediaEntity->getFiles();
        if (!isset($files[0])) {
            throw new FileNotFoundException('File was not found in media entity with the id . ' . $data['id']);
        }
        /** @var File $file */
        $file = $files[0];
        // currently a media can only have one file
        $file->setChanger($user);
        $file->setChanged(new \DateTime());
        $version = $file->getVersion();
        $currentFileVersion = null;
        foreach ($file->getFileVersions() as $fileVersion) {
            /** @var FileVersion $fileVersion */
            if ($version == $fileVersion->getVersion()) {
                $currentFileVersion = $fileVersion;
                break;
            }
        }
        if (!$currentFileVersion) {
            throw new FileVersionNotFoundException($mediaEntity->getId(), $version);
        }
        if ($uploadedFile) {
            // new uploaded file
            ++$version;
            $this->validator->validate($uploadedFile);
            $type = $this->typeManager->getMediaType($uploadedFile->getMimeType());
            if ($type !== $mediaEntity->getType()->getId()) {
                throw new InvalidMediaTypeException('New media version must have the same media type.');
            }
            $data['storageOptions'] = $this->storage->save($uploadedFile->getPathname(), $this->getNormalizedFileName($uploadedFile->getClientOriginalName()), $version, $currentFileVersion->getStorageOptions());
            $data['name'] = $uploadedFile->getClientOriginalName();
            $data['size'] = intval($uploadedFile->getSize());
            $data['mimeType'] = $uploadedFile->getMimeType();
            $data['properties'] = $this->getProperties($uploadedFile);
            $data['type'] = ['id' => $type];
            $data['version'] = $version;
            $fileVersion = clone $currentFileVersion;
            $this->em->persist($fileVersion);
            $fileVersion->setChanged(new \DateTime());
            $fileVersion->setChanger($user);
            $fileVersion->setCreated(new \DateTime());
            $fileVersion->setCreator($user);
            $fileVersion->setDownloadCounter(0);
            $file->setVersion($version);
            $fileVersion->setVersion($version);
            $fileVersion->setFile($file);
            $file->addFileVersion($fileVersion);
            // delete old fileversion from cache
            $this->formatManager->purge($mediaEntity->getId(), $currentFileVersion->getName(), $currentFileVersion->getStorageOptions());
        } else {
            // not setable in update
            $data['name'] = null;
            $data['size'] = null;
            $data['type'] = null;
            $data['version'] = null;
            $data['mimeType'] = null;
            $data['storageOptions'] = null;
            $data['changed'] = date('Y-m-d H:i:s');
            if (isset($data['focusPointX']) && $data['focusPointX'] != $currentFileVersion->getFocusPointX() || isset($data['focusPointY']) && $data['focusPointY'] != $currentFileVersion->getFocusPointY()) {
                $currentFileVersion->increaseSubVersion();
                $this->formatManager->purge($mediaEntity->getId(), $currentFileVersion->getName(), $currentFileVersion->getStorageOptions());
            }
        }
        $media = new Media($mediaEntity, $data['locale'], null);
        $media = $this->setDataToMedia($media, $data, $user);
        $this->em->persist($media->getEntity());
        $this->em->flush();
        return $media;
    }