Scalr\Api\Service\User\V1beta0\Adapter\ImageAdapter::validateEntity PHP Method

validateEntity() public method

See also: Scalr\Api\DataType\ApiEntityAdapter::validateEntity()
public validateEntity ( $entity )
    public function validateEntity($entity)
    {
        if (!$entity instanceof Entity\Image) {
            throw new \InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\Image class"));
        }
        if ($entity->hash !== null) {
            //Checks if the image does exist
            if (!Entity\Image::findPk($entity->hash)) {
                throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the Image with ID: %d", $entity->hash));
            }
        } else {
            $image = Entity\Image::findOne([['id' => $entity->id], ['platform' => $entity->platform], ['cloudLocation' => (string) $entity->cloudLocation], ['$or' => [['accountId' => null], ['$and' => [['accountId' => $entity->accountId], ['$or' => [['envId' => null], ['envId' => $entity->envId]]]]]]]]);
            if ($image) {
                throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, "This Image has already been registered in Scalr");
            }
        }
        //Is this a new Image
        if (!$entity->hash) {
            $entity->createdByEmail = $this->controller->getUser()->email;
            $entity->createdById = $this->controller->getUser()->id;
        }
        if (!Entity\Role::isValidName($entity->name)) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid name of the Image");
        }
        if (empty($entity->architecture)) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property 'architecture'");
        }
        if (!$this->controller->hasPermissions($entity, true)) {
            //Checks entity level write access permissions
            throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "Insufficient permissions");
        }
        //We only allow to either create or modify Environment Scope Roles
        if ($entity->getScope() !== $this->controller->getScope()) {
            throw new ApiErrorException(403, ErrorMessage::ERR_SCOPE_VIOLATION, sprintf("Invalid scope"));
        }
        if (empty($entity->osId)) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property 'os.id'");
        }
        //Tries to find out the specified OS
        if (empty(Entity\Os::findPk($entity->osId))) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "OS with id '{$entity->osId}' not found.");
        }
        if (empty($entity->platform)) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property platform");
        }
        if (!isset(SERVER_PLATFORMS::GetList()[$entity->platform])) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Unexpected platform value");
        }
    }