Exemplo n.º 1
0
 /**
  * Validates a product and the relative uploaded file
  * Eventually sets the model's success and error messages (if any)
  * @param Product $product
  * @param UploadedFile $uploadedFile
  * @param ProductCreationModel $model
  */
 private function validateProduct(Product $product, UploadedFile $uploadedFile, ProductCreationModel $model)
 {
     // Validation rules:
     // Product must have a name
     // Product must have at least one tag
     // (extra) Product image, if present, must be an image (gif, png, jpeg)
     if (!$product->getName()) {
         $model->appendMessage("The product's name mustn't be empty!");
     }
     if (!$product->getTags()) {
         $model->appendMessage("The product must have at least one tag!");
     }
     if ($uploadedFile->getSize() && !in_array($uploadedFile->getClientMediaType(), static::$supportedImageMIMETYpes)) {
         $model->appendMessage("The product's image file is invalid (wrong type)!");
     }
     // if the model's messages are empty, validation passed
     $model->setIsSuccess(!$model->getMessages());
 }
 public function getName()
 {
     $this->__load();
     return parent::getName();
 }
Exemplo n.º 3
0
 /**
  * @param Product $product the product to update
  * @return boolean true on success
  * @throws NotFoundException if not found
  */
 public function update($product)
 {
     $productId = $product->getId();
     if ($productId === null || $productId === 0) {
         $this->logger->error(__METHOD__ . ": Can't update a non-existing product (id: {$productId})");
         return false;
     }
     $repository = $this->getRepository();
     if ($repository == null) {
         $this->logger->error(__METHOD__ . ": No repository, impossible to proceed.");
         return false;
     }
     /** @var Product $dbOne */
     $dbOne = $repository->find($productId);
     if ($dbOne === null) {
         throw new NotFoundException("Product (id: {$productId}) was not found, impossible to update.");
     }
     $dbOne->setDescription($product->getDescription())->setName($product->getName())->setTags($product->getTags());
     $em = $this->getEntityManager();
     if ($em == null) {
         $this->logger->error(__METHOD__ . ": No entity manager, impossible to proceed.");
         return false;
     }
     try {
         $em->persist($dbOne);
         $em->flush();
         return true;
     } catch (ORMInvalidArgumentException $ex) {
         $this->logger->error(__METHOD__ . ": (" . $ex->getCode() . ") " . $ex->getMessage());
         return false;
     }
 }