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

validateEntity() public method

See also: Scalr\Api\DataType\ApiEntityAdapter::validateEntity()
public validateEntity ( $entity )
    public function validateEntity($entity)
    {
        if (!$entity instanceof Entity\Role) {
            throw new \InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\Role class"));
        }
        if ($entity->id !== null) {
            if (!is_integer($entity->id)) {
                throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid value of the identifier");
            }
            //Checks if the role does exist
            if (!Entity\Role::findPk($entity->id)) {
                throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the Role with ID: %d", $entity->id));
            }
        }
        //Is this a new Role
        if (!$entity->id) {
            $entity->addedByEmail = $this->controller->getUser()->email;
            $entity->addedByUserId = $this->controller->getUser()->id;
        }
        if (!Role::isValidName($entity->name)) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid name of the Role");
        }
        $envId = $this->controller->getScope() === ScopeInterface::SCOPE_ENVIRONMENT ? $this->controller->getEnvironment()->id : null;
        if (Role::isNameUsed($entity->name, $this->controller->getUser()->accountId, $envId, $entity->id)) {
            throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, 'Selected role name is already used. Please select another one.');
        }
        $entity->description = $entity->description ?: '';
        $this->validateString($entity->description, 'Invalid description');
        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"));
        }
        //Checks the Role Category
        if (!empty($entity->catId)) {
            //Tries to find out the specified Role category
            $category = Entity\RoleCategory::findPk($entity->catId);
            if ($category instanceof Entity\RoleCategory) {
                //Checks if the specified RoleCategory either shared or belongs to User's scope.
                if ($category->getScope() !== ScopeInterface::SCOPE_SCALR && $category->envId !== $this->controller->getEnvironment()->id) {
                    throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "The specified category isn't owned by your environment.");
                }
            } else {
                throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "The Role category does not exist");
            }
        } else {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Role category should be provided with the request.");
        }
        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}' is not found.");
        }
        if (empty($entity->isScalarized)) {
            if (!empty($entity->behaviors)) {
                throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, 'builtinAutomation property requires Scalr agent to be set');
            }
        } else {
            if (empty($entity->behaviors)) {
                throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property builtinAutomation");
            }
        }
    }