Pony.fm/app/models/Commands/CreateAlbumCommand.php

65 lines
1.6 KiB
PHP
Raw Normal View History

2013-07-28 10:35:31 +02:00
<?php
namespace Commands;
use Entities\Album;
use Entities\Image;
use Entities\Track;
use External;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
2013-07-28 19:45:21 +02:00
use Illuminate\Support\Facades\Validator;
2013-07-28 10:35:31 +02:00
class CreateAlbumCommand extends CommandBase {
private $_input;
function __construct($input) {
$this->_input = $input;
}
/**
* @return bool
*/
public function authorize() {
$user = \Auth::user();
return $user != null;
}
/**
* @throws \Exception
* @return CommandResponse
*/
public function execute() {
$rules = [
'title' => 'required|min:3|max:50',
'cover' => 'image|mimes:png|min_width:350|min_height:350',
'cover_id' => 'exists:images,id',
2013-07-28 19:45:21 +02:00
'track_ids' => 'exists:tracks,id'
2013-07-28 10:35:31 +02:00
];
2013-07-28 19:45:21 +02:00
$validator = Validator::make($this->_input, $rules);
2013-07-28 10:35:31 +02:00
if ($validator->fails())
return CommandResponse::fail($validator);
$album = new Album();
$album->user_id = Auth::user()->id;
$album->title = $this->_input['title'];
$album->description = $this->_input['description'];
if (isset($this->_input['cover_id'])) {
$album->cover_id = $this->_input['cover_id'];
}
else if (isset($this->_input['cover'])) {
$cover = $this->_input['cover'];
$album->cover_id = Image::upload($cover, Auth::user())->id;
2013-07-28 19:45:21 +02:00
} else if (isset($this->_input['remove_cover']) && $this->_input['remove_cover'] == 'true')
2013-07-28 10:35:31 +02:00
$album->cover_id = null;
2013-07-28 19:45:21 +02:00
$trackIds = explode(',', $this->_input['track_ids']);
2013-07-28 10:35:31 +02:00
$album->save();
2013-07-28 19:45:21 +02:00
$album->syncTrackIds($trackIds);
2013-07-28 10:35:31 +02:00
return CommandResponse::succeed(['id' => $album->id]);
}
}