/**
  * Execute the command.
  *
  * @param ContentType $contentType
  * @param ContentTypeTaxonomy $contentTypeTaxonomy
  * @param Validator $validator
  * @param Dispatcher $dispatcher
  * @return CommandResult
  */
 public function handle(ContentType $contentType, ContentTypeTaxonomy $contentTypeTaxonomy, Validator $validator, Dispatcher $dispatcher)
 {
     // in order to determine what permissions are needed to create
     // a taxonomy terms, we will get first what taxonomy the term is for
     // after we can get the taxonomy, we will then get what type the taxonomy
     // belong so we can verify if the user has permission for that type
     try {
         $taxonomy = $contentTypeTaxonomy->findOrFail($this->contentTypeTaxonomyId);
     } catch (\Exception $e) {
         return new CommandResult(false, "Invalid Taxonomy.", null, 400);
     }
     try {
         $type = $contentType->findOrFail($taxonomy->content_type_id);
     } catch (\Exception $e) {
         return new CommandResult(false, "Invalid Content Type.", null, 400);
     }
     // build the permissions needed
     $canManageOnThisType = $type->type . '.manage';
     // check if user has permission
     if (!$this->disablePermissionChecking) {
         if (!$this->user->hasAnyPermission([$canManageOnThisType])) {
             return new CommandResult(false, "Not enough permission.", null, 403);
         }
     }
     // validate data
     $validationResult = $validator->make(array('term' => $this->term, 'slug' => $this->slug, 'content_type_taxonomy_id' => $this->contentTypeTaxonomyId), ContentTypeTaxonomyTerm::$rules);
     if ($validationResult->fails()) {
         return new CommandResult(false, $validationResult->getMessageBag()->first(), null, 400);
     }
     // prepare term to be created
     $termToBeCreated = array('term' => $this->term, 'slug' => $this->slug, 'content_type_taxonomy_id' => $this->contentTypeTaxonomyId);
     // fire creating event
     $dispatcher->fire('contentTypeTaxonomyTerm.creating', array($termToBeCreated));
     // store
     $createdTerm = $taxonomy->terms()->create($termToBeCreated);
     // fire creating event
     $dispatcher->fire('contentTypeTaxonomyTerm.created', array($createdTerm));
     // all good
     return new CommandResult(true, "Taxonomy term successfully created.", $createdTerm, 201);
 }
 /**
  * @param ContentType $contentType
  * @param ContentTypeTaxonomy $contentTypeTaxonomy
  * @param ContentTypeTaxonomyTerm $contentTypeTaxonomyTerm
  * @param Dispatcher $dispatcher
  * @return CommandResult
  */
 public function handle(ContentType $contentType, ContentTypeTaxonomy $contentTypeTaxonomy, ContentTypeTaxonomyTerm $contentTypeTaxonomyTerm, Dispatcher $dispatcher)
 {
     // in order to determine what permissions are needed to create
     // a taxonomy terms, we will get first what taxonomy the term is for
     // after we can get the taxonomy, we will then get what type the taxonomy
     // belong so we can verify if the user has permission for that type
     try {
         $taxonomy = $contentTypeTaxonomy->findOrFail($this->taxonomyId);
     } catch (\Exception $e) {
         return new CommandResult(false, "Invalid Taxonomy.", null, 400);
     }
     try {
         $type = $contentType->findOrFail($taxonomy->content_type_id);
     } catch (\Exception $e) {
         return new CommandResult(false, "Invalid Content Type.", null, 400);
     }
     try {
         $term = $contentTypeTaxonomyTerm->findOrFail($this->termId);
     } catch (\Exception $e) {
         return new CommandResult(false, "Invalid Taxonomy Term.", null, 400);
     }
     // build the permissions needed
     $canManageOnThisType = $type->type . '.manage';
     // check if user has permission
     if (!$this->disablePermissionChecking) {
         if (!$this->user->hasAnyPermission([$canManageOnThisType])) {
             return new CommandResult(false, "Not enough permission.", null, 403);
         }
     }
     // fire creating event
     $dispatcher->fire('taxonomyTerm.deleting', array($this->args));
     // detach all posts type related to this term first
     $term->contents()->detach();
     $term->delete();
     // fire creating event
     $dispatcher->fire('taxonomyTerm.deleted', array($taxonomy));
     // all good
     return new CommandResult(true, "Taxonomy Term successfully deleted.", null, 200);
 }