Scalr\Api\Service\User\V1beta0\Controller\Roles::replaceImageAction PHP Method

replaceImageAction() public method

Associates a new Image with the Role
public replaceImageAction ( integer $roleId, string $imageId ) : Scalr\Api\DataType\ResultEnvelope
$roleId integer The Identifier of the role
$imageId string The Identifier of the image
return Scalr\Api\DataType\ResultEnvelope
    public function replaceImageAction($roleId, $imageId)
    {
        $this->checkScopedPermissions('ROLES', 'MANAGE');
        $role = $this->getRole($roleId, true, true);
        $oldImage = $this->getImage($roleId, $imageId);
        $object = $this->request->getJsonBody();
        $objectImageId = static::getBareId($object, 'image');
        $objectRoleId = static::getBareId($object, 'role');
        if (empty($objectImageId)) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Invalid body");
        }
        if (!preg_match('/' . ApiApplication::REGEXP_UUID . '/', $objectImageId) || $oldImage->hash == $objectImageId) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid image identifier");
        }
        if (!empty($objectRoleId) && $roleId != $objectRoleId) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid identifier of the role");
        }
        if (is_object($object->image)) {
            $imageAdapter = $this->adapter('image');
            //Pre validates the request object
            $imageAdapter->validateObject($object->image);
        }
        $criteria = $this->getScopeCriteria();
        $criteria[] = ['hash' => $objectImageId];
        /* @var $image Entity\Image */
        $image = Entity\Image::findOne($criteria);
        if (empty($image)) {
            throw new ApiErrorException(404, ErrorMessage::ERR_INVALID_VALUE, "The Image either does not exist or isn't in scope for the current Environment.");
        }
        if ($image->cloudLocation !== $oldImage->cloudLocation) {
            throw new ApiErrorException(400, ErrorMessage::ERR_BAD_REQUEST, "You can only replace images with equal cloud locations");
        }
        $this->setImage($role, $image->platform, $image->cloudLocation, $image->id, $this->getUser()->id, $this->getUser()->email);
        $this->response->setStatus(200);
        return $this->result(['image' => ['id' => $image->hash], 'role' => ['id' => $role->id]]);
    }