001/*
002 *   Copyright 2024 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.verify2;
017
018import com.fasterxml.jackson.annotation.JsonProperty;
019import com.vonage.client.JsonableBaseObject;
020import java.util.Objects;
021
022/**
023 * Base workflow.
024 */
025public class Workflow extends JsonableBaseObject {
026        protected final Channel channel;
027        protected String to, from;
028
029        protected Workflow(Builder<?, ?> builder) {
030                this(builder.channel, builder.to, builder.from);
031        }
032
033        protected Workflow(Channel channel, String to) {
034                this(channel, to, null);
035        }
036
037        protected Workflow(Channel channel, String to, String from) {
038                this.channel = Objects.requireNonNull(channel, "Verification channel is required.");
039                this.to = validateTo(to);
040                this.from = validateFrom(from);
041        }
042
043        protected String validateTo(String to) {
044                if ((this.to = to) == null || to.trim().isEmpty()) {
045                        throw new IllegalArgumentException("Recipient is required.");
046                }
047                return to;
048        }
049
050        protected String validateFrom(String from) {
051                if (from != null && from.trim().length() < 3) {
052                        throw new IllegalArgumentException("Sender must be at least 3 characters long.");
053                }
054                return from;
055        }
056
057        /**
058         * The communication channel for this verification workflow.
059         *
060         * @return The channel as an enum.
061         */
062        @JsonProperty("channel")
063        public Channel getChannel() {
064                return channel;
065        }
066
067        /**
068         * The phone number to contact with the message.
069         *
070         * @return The recipient's phone number, in E.164 format.
071         */
072        @JsonProperty("to")
073        public String getTo() {
074                return to;
075        }
076
077        /**
078         * Builder class for an SMS workflow.
079         *
080         * @since 8.2.0
081         */
082        @SuppressWarnings("unchecked")
083        protected abstract static class Builder<W extends Workflow, B extends Builder<? extends W, ? extends B>>  {
084                protected final Channel channel;
085                protected String to, from;
086
087                protected Builder(Channel channel) {
088                        this.channel = channel;
089                }
090
091                protected B to(String to) {
092                        this.to = to;
093                        return (B) this;
094                }
095
096                protected B from(String from) {
097                        this.from = from;
098                        return (B) this;
099                }
100
101                /**
102                 * Builds the workflow.
103                 *
104                 * @return A new instance of the workflow with this builder's fields.
105                 */
106                public abstract W build();
107        }
108}