Skip to content

Commit

Permalink
SW-18242 - add parameters to exceptions in media resource
Browse files Browse the repository at this point in the history
  • Loading branch information
mitelg committed Jun 30, 2017
1 parent 34b846b commit 3b6169c
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions engine/Shopware/Components/Api/Resource/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Media extends Resource
*/
public function getRepository()
{
return $this->getManager()->getRepository('Shopware\Models\Media\Media');
return $this->getManager()->getRepository(MediaModel::class);
}

/**
Expand All @@ -56,7 +56,7 @@ public function getRepository()
* @throws \Shopware\Components\Api\Exception\ParameterMissingException
* @throws \Shopware\Components\Api\Exception\NotFoundException
*
* @return array|\Shopware\Models\Media\Media
* @return array|MediaModel
*/
public function getOne($id)
{
Expand All @@ -69,7 +69,7 @@ public function getOne($id)
$filters = [['property' => 'media.id', 'expression' => '=', 'value' => $id]];
$query = $this->getRepository()->getMediaListQuery($filters, [], 1);

/** @var $media \Shopware\Models\Media\Media */
/** @var $media MediaModel */
$media = $query->getOneOrNullResult($this->getResultMode());

if (!$media) {
Expand Down Expand Up @@ -119,15 +119,15 @@ public function getList($offset = 0, $limit = 25, array $criteria = [], array $o
* @throws \Shopware\Components\Api\Exception\ValidationException
* @throws \Exception
*
* @return \Shopware\Models\Media\Media
* @return MediaModel
*/
public function create(array $params)
{
$this->checkPrivilege('create');

$params = $this->prepareMediaData($params);

$media = new \Shopware\Models\Media\Media();
$media = new MediaModel();
$media->fromArray($params);

$path = $this->prepareFilePath($media->getPath(), $media->getFileName());
Expand Down Expand Up @@ -160,17 +160,17 @@ public function create(array $params)
* @throws \Shopware\Components\Api\Exception\ParameterMissingException
* @throws \Shopware\Components\Api\Exception\CustomValidationException
*
* @return \Shopware\Models\Media\Media
* @return MediaModel
*/
public function update($id, array $params)
{
$this->checkPrivilege('update');

if (empty($id)) {
throw new ApiException\ParameterMissingException();
throw new ApiException\ParameterMissingException('id');
}

/** @var $media \Shopware\Models\Media\Media */
/** @var $media MediaModel */
$media = $this->getRepository()->find($id);

if (!$media) {
Expand Down Expand Up @@ -203,17 +203,17 @@ public function update($id, array $params)
* @throws \Shopware\Components\Api\Exception\ParameterMissingException
* @throws \Shopware\Components\Api\Exception\NotFoundException
*
* @return \Shopware\Models\Media\Media
* @return MediaModel
*/
public function delete($id)
{
$this->checkPrivilege('delete');

if (empty($id)) {
throw new ApiException\ParameterMissingException();
throw new ApiException\ParameterMissingException('id');
}

/** @var $media \Shopware\Models\Media\Media */
/** @var $media MediaModel */
$media = $this->getRepository()->find($id);

if (!$media) {
Expand Down Expand Up @@ -256,7 +256,7 @@ public function internalCreateMediaByFileLink($link, $albumId = -1)
$media->setUserId(0);

/** @var $album Album */
$album = $this->getManager()->find('Shopware\Models\Media\Album', $albumId);
$album = $this->getManager()->find(Album::class, $albumId);
if (!$album) {
throw new ApiException\CustomValidationException(
sprintf('Album by id %s not found', $albumId)
Expand Down Expand Up @@ -430,8 +430,8 @@ protected function uploadBase64File($url, $destinationPath, $baseFilename)
}

/**
* @param array $params
* @param \Shopware\Models\Media\Media $media
* @param array $params
* @param MediaModel $media
*
* @throws \Shopware\Components\Api\Exception\CustomValidationException
* @throws \Shopware\Components\Api\Exception\ParameterMissingException
Expand All @@ -443,15 +443,15 @@ private function prepareMediaData($params, $media = null)
{
// in create mode, album is a required param
if (!$media && (!isset($params['album']) || empty($params['album']))) {
throw new ApiException\ParameterMissingException();
throw new ApiException\ParameterMissingException('album');
}

if (!$media && (!isset($params['file']) || empty($params['file']))) {
throw new ApiException\ParameterMissingException();
throw new ApiException\ParameterMissingException('file');
}

if (!$media && (!isset($params['description']) || empty($params['description']))) {
throw new ApiException\ParameterMissingException();
throw new ApiException\ParameterMissingException('description');
}

if (!$media && (!isset($params['userId']) || empty($params['userId']))) {
Expand All @@ -464,14 +464,14 @@ private function prepareMediaData($params, $media = null)

// Check / set album
if (isset($params['album'])) {
$album = Shopware()->Models()->find('\Shopware\Models\Media\Album', $params['album']);
$album = Shopware()->Models()->find(Album::class, $params['album']);
if (!$album) {
throw new ApiException\CustomValidationException(sprintf('Album by id %s not found', $params['album']));
}
$params['album'] = $album;
}

if (isset($params['file']) && !($params['file'] instanceof \Symfony\Component\HttpFoundation\File\File)) {
if (isset($params['file']) && !($params['file'] instanceof File)) {
if (!isset($params['name'])) {
$params['name'] = pathinfo($params['file'], PATHINFO_FILENAME);
}
Expand All @@ -486,7 +486,7 @@ private function prepareMediaData($params, $media = null)
} else {
$path = $params['file'];
}
$params['file'] = new \Symfony\Component\HttpFoundation\File\File($path);
$params['file'] = new File($path);
}

return $params;
Expand Down

0 comments on commit 3b6169c

Please sign in to comment.