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

validateEntity() public method

See also: ApiEntityAdapter::validateEntity()
public validateEntity ( $entity )
    public function validateEntity($entity)
    {
        if (!$entity instanceof Entity\ScalingMetric) {
            throw new InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\ScalingMetric class"));
        }
        if (empty($entity->name)) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Missed property name');
        }
        if (!preg_match('/^' . Entity\ScalingMetric::NAME_REGEXP . '$/', $entity->name)) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, 'Scaling metric name should be both alphanumeric and greater than 5 chars');
        }
        $criteria = $this->controller->getScopeCriteria();
        $criteria[] = ['name' => $entity->name];
        $criteria[] = ['id' => ['$ne' => $entity->id]];
        if (!empty(Entity\ScalingMetric::findOne($criteria))) {
            throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, sprintf('Scaling metric with name %s already exists', $entity->name));
        }
        if (empty($entity->retrieveMethod)) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Missed property retrieveMethod');
        }
        if (!in_array($entity->retrieveMethod, [Entity\ScalingMetric::RETRIEVE_METHOD_EXECUTE, Entity\ScalingMetric::RETRIEVE_METHOD_READ, Entity\ScalingMetric::RETRIEVE_METHOD_URL_REQUEST])) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, 'Unexpected retrieveMethod value');
        }
        $uriParts = parse_url($entity->filePath);
        if (!$uriParts) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, 'Property filePath is invalid');
        }
        if ($entity->retrieveMethod == Entity\ScalingMetric::RETRIEVE_METHOD_URL_REQUEST && (empty($uriParts['scheme']) || !in_array($uriParts['scheme'], ['http', 'https']))) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, 'Invalid path for request.');
        }
        if (empty($entity->calcFunction)) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Missed property function');
        }
        if (empty($entity->filePath)) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Missed property filePath');
        }
    }